Home >Web Front-end >JS Tutorial >Summary of methods for manipulating strings in JS
1: concat() combines two or more characters of text and returns a new string
1 | var f1 ="hello"; |
2 | var f2="world"; |
3 | document.write(f1.concat(f2)) //hello world |
2:indexof() Returns the index of the first occurrence of a substring in the string. If there is no match, returns -1
var f3="hello world" | |
console.log(f3.indexOf('world')) //6 | |
console.log(f3.indexOf(' World')) //-1 | |
##console.log(f3.indexOf('hello')) / /0 |
Of is case sensitive
2 | |
3 | |
2 | |
##3 | |
4 | |
5:substring() – Returns a substring of a string. The parameters passed in are the starting position and the ending position (not required). Note: The parameter cannot be a negative number |
##1
2 | console.log(f5.substring(3)) //lo world |
3 | console.log(f5.substring(3,8) ) //lo wo |
6:match() – Checks whether a string matches a regular expression. |
2 | console.log(f6.slice(6)) //world |
3 | console.log(f6.slice(6,9)) //wor |
#10: split() – method is used to split a string into an array of strings. |
2 | console .log(f7.split("")); //["h", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d"] |
3 | console.log(f7.split(" ")); //["hello", "world" ] |
4 | console.log(f7.split(" ",1)); //["hello"] |
11:toLowerCase() – Convert the entire string to lowercase letters. | 12:toUpperCase() – Convert the entire string to uppercase letters. |
The above is the detailed content of Summary of methods for manipulating strings in JS. For more information, please follow other related articles on the PHP Chinese website!