JavaScript string cut split
The most common method of string cutting in JavaScript is split()
this function. This function returns an array after the cut.
Example:
var"hello world";var arr = str.split(" "//用空格切割字符串。alert( arr[0// 输出 helloalert( arr[1//输出 world
This is relatively simple.
Substring
This is also used to cut strings, but based on location, rather than character characteristics.
Example:
"iamalps1992"sub = str.substring(7, 11); //获取第7个字符到第11个(不包括第11个字符)的字符串alert(sub); //输出:1992
This is useful in many cases, especially when intercepting a part of a string.
IndexOf
This is the method of finding, which substring
provides parameters to the method above. Returns the index subscript for the first occurrence of the found position.
Example:
var"Iamalps1992";var first = str.indexOf("a"// first的值为 1var second = str.indexOf("alps"// second的值为 3var third = str.indexOf("z"// third的值为 -1
The above is the way JavaScript cuts strings.
JavaScript Learning-String Cut lookup