String segmentation; string truncation; string search; string specified position insertion; string and array conversion 1. Segmentation
StingObj. split ([separator [, limit])
Parameter description:
Separator
Optional. A string or regular expression object that identifies whether a string is separated by one or more characters. If this option is ignored, a single array of elements containing the entire string is returned.
Limit
Optional. This value is used to limit the number of elements in the returned array.
Note:
The result of the split method is a string array. In stingObj, the position where separator appears must be decomposed. The separator is not returned as part of any array element.
Instance
Var str = "tobeornottobethisis"; console. log (str. split ("o"); // ["t", "be", "rn", "tt", "bethisis"] console. log (str. split ("to"); // ["", "beornot", "bethisis"] console. log (str. split ("t", 2); // ["", "obeorno"] // split is generally used with join, convert the array into a string // remove the string contained in the string beconsole. log (str. split ("be "). join (''); // toornottothisis2. truncate 1. Extract characters
Use str [index] to extract characters at a specified position
Instance:
var str="tobeornottobethisis";console.log(str[3]);//e
2. Extract substrings
1. Obtain the substring by specifying the subscript range
StingObj. substring (start, [end])
Parameter description:
Start
Specifies the starting position of the substring. The index starts from 0.
End
Specifies the end position of the substring, but does not contain the end character. If the end position is not specified, all strings from start to end are returned.
NOTE: If start is greater than end, the Child string before end to start is returned.
Instance
var str="tobeornottobethisis";console.log(str.substring(0,3));//tobconsole.log(str.substring(2,3));//bconsole.log(str.substring(5,3));//eoconsole.log(str.substring(3,0));tob
Obtain the substring by specifying the start position and length of the substring.
StingObj. substr(start [, length ])
Parameter description
Start
Required. The starting position of the required substring. The index of the first character in the string is 0.
Length
Optional. The number of characters to be included in the returned substring.
NOTE: IfLengthIf it is 0 or negative, an empty string is returned. If this parameter is not specified, the substring is extendedStringvarEnd
Instance
Var str = "tobeornottobethisis"; console. log (str. substr (0, 3); // tobconsole. log (str. substr (3); // eornottobethisisconsole. log (str. substr (3, 0); // returns an empty string console. log (str. substr (1,-3); // returns an empty string
3. Search
Check whether strObj contains subString.
strObj.indexOf(subString[, startIndex])
Parameter description
StrObj
Required.StringObject or text.
SubString
Required. ToStringThe substring found in the object.
StarIndex
Optional. This integer indicates thatStringIndex in the object. If it is omitted, It is searched from the beginning of the string.
Note:
The indexOf method returns an integer indicating the starting position of the substring in the String object. If no substring is found,-1 is returned.
If startindex is negative, startindex is treated as zero. If it is larger than the index at the largest character location, it is regarded as the largest possible index.
Instance:
var str="tobeornottobethisis";console.log(str.indexOf("tto"));//8console.log(str.indexOf("tto",7));//8console.log(str.indexOf("tto",10));//-1console.log(str.indexOf("tto",-1));//8console.log(str.indexOf("tto",25));//-1console.log(str.indexOf("ag"));//-14. Insert at the specified position
I believe that the first thought in my mind is through str [I] = subStr; but this is not the case.
Correct Solution
Method 1:
var str="tobeornottobethisis";var substr="what";function fn(str,substr,index){var leftStr=str.substring(0,index);var rightStr=str.substr(index);return leftStr+substr+rightStr;}console.log(fn(str,substr,2));
Method 2:
Function fn2 (str, substr, index) {var strLen = str. length; var str = str. split (''); // split it into an array if (index
Method 3:
Function fn3 (str, substr, index) {var strLen = str. length; var substrLen = substr. length; var curIndex = strLen + substrLen; var result = ''; var sepIndex = index + substrLen; for (var I = 0, j = 0; I
The first or second method is recommended.
3. Conversion of strings and Arrays
1. Convert the array into a string
arrayObj.join(separator)
Parameter description
ArrayObj
Required.ArrayObject.
Separator
Optional. IsStringObject as the finalStringThe delimiter between array elements in the object. If this parameter is omitted, an array element is separated by a comma.
NOTE: If any element in the array is not defined orNullAnd treat it as a null string.
Instance
var arr=['women','hello','findyou','seekyou'];console.log(arr.join());//women,hello,findyou,seekyouconsole.log(arr.join('-'));//women-hello-findyou-seekyouconsole.log(arr.join("(*)"));//women(*)hello(*)findyou(*)seekyou
2. Convert string to array
Split is used to split and convert the string into an array. This section has been discussed and is not described in detail.
The above is a summary of common operations on strings in JavaScript. For more information, see PHP Chinese website (www.php1.cn )!