1. String conversion toString (): You can convert any type of data to a string
var num= 19; 19
var mystr = num.tostring (); "19"
var mystr = String (num); "19"
var mystr = "" + num; "19
2, String split split (","): divides the string into bits multiple strings, split () the second parameter, which represents the maximum length of the returned string array
var str = "I,love,you,do,you,love,me"
var
strArr = myStr .split(
","
);
// ["I", "Love", "You", "Do", "you", "love", "me"];
var
strLim = myStr .split(
","
, 3);
// ["I", "Love", "You"];
3. String cuttingSlice (i,j), substring (i,j)The truncated character should be in the second argument before a "i,j" var str = "I,love,you,do,you,love,me" var slice = str.slice (1,5); ", Lov" varsubstring= Str.substring (1,5); ", Lov" varsubstr= Str.substr (1,5); ", Love" substr (i,j) The second parameter represents " the maximum length of a truncated string " 4. Find the character at the given positioncharAt (8)
var
myStr =
"I,love,you,Do,you,love,me"
;
var
theChar = myStr.charAt(8);
// "o",同样从0开始
5. Query substring Str.indexof ()
The first function:indexOf (), which starts from the beginning of the string, finds the corresponding coordinates, and cannot find the return-1. Following
var
myStr =
"I,Love,you,Do,you,love,me"
;
var
index = myStr.indexOf(
"you"
);
// 7 ,基于0开始,找不到返回-1
The second function:lastIndexOf (), which starts at the end of the string and finds the corresponding coordinates returned, cannot find return-1. As follows:
var
myStr =
"I,Love,you,Do,you,love,me"
;
var
index = myStr.lastIndexOf(
"you"
);
// 14
Manipulating functions and methods of strings in JavaScript