JS method commonly used in the string

The method of the charAt // () takes an integer as a parameter, comprising a return character at the specified position.
STR = var 'smileGate';
var R & lt str.charAt = (. 1);
the console.log (R & lt); // m

// Method substring () the two index as parameters, and returns the extracted sub-string between the two indexes. Where the character contains a starting index, the ending index does not contain character is located, the second argument is omitted, substring the extract substring from the end of the string to the specified index.
the console.log (str.substring (2,5)); // Ile
the console.log (str.substring (2)); // ileGate

// indexOf method takes a string as a parameter and returns the index of the parameter of the first character in the string in the parameter first appears. Specifies the second parameter. It is an index, specify from which position you start looking. If the specified string is not found, returns -1.
Phone = var "867-5309";
var index = phone.indexOf ( "-");
the console.log (index); //. 3

var phrase = "the cat in the hat";
var index1 = phrase.indexOf("cat");
console.log(index1); //4
var index2 = phrase.indexOf("the", 5);
console.log(index2); //11

// split () is used as a delimiter character as a parameter, and based on this delimiter string array into a plurality of parts
var Data = "name | Phone | address";
var = data.split Vals ( "| ");
the console.log (Vals); // [" name "," Phone "," address "]

// replace () Find substring and replaces them with another string.
console.log (data.replace ( 'phone', 'age')); // name | age | address

// slice () method may extract a portion of the string, and return a new string portion is extracted (extraction rule the same substring).
console.log (data.slice (5, 8)); // Pho
console.log (data.slice (5)); // Phone | address
console.log (data.substring (5, 8)); // Pho
console.log (data.substring (5)); // Phone | address

// lastIndexOf () and indexOf Similarly, a return index of the last substring matching.
console.log (data.lastIndexOf ( 'a') ); // 11

// concat () string spliced together.
str1 = var 'ABC';
var str2 = 'DEF';
the console.log (str1.concat (str2)); // abcdef

// trim () to delete the beginning and end a string of blank characters
// match () to find the regular expression matching substring in the string.
// toLowerCase () toUpperCase () to change the case

/ * slice () and substring () method distinction
str.slice (start, end)
if the start or end are negative, as their length + start / length + end where length is the length of the array. If you omit end, then slice extraction continues to the end of str. If end occurs before start, do not copy any elements to the new array.
str.substring (start, end)
if the start or end NaN or negative, then it is replaced by 0.
Substring length equal to the absolute difference between the start and the end. For example, the length str.substring (0, 3), and str.substring (3, 0) returns the substring is 3.
* /

var str3 = 'helloworld';
console.log(str3.slice(-1,10)); //d
console.log(str3.slice(-1,2)); //空

console.log(str3.substring(0,3)); //hel
console.log(str3.substring(3,0)); //hel
console.log(str3.substring(-2,3)); //hel

Guess you like

Origin www.cnblogs.com/xiadengqi/p/12403566.html