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
##1 | var f3="hello world" |
2 | console.log(f3.indexOf('world')) //6 |
3 | console.log(f3.indexOf(' World')) //-1 |
4 |
##console.log(f3.indexOf('hello')) / /0 |
##Note
: index
Of is case sensitive
3:charAt() – Returns the character at the specified position.
1
var f4="hello world"; |
| 2
console.log(f4.charAt(1)) //e |
| 3
console.log(f4) //hello world |
|
4:lastIndexOf() method can return the last occurrence position of a specified string value, and search from back to front at the specified position in a string .
1
var f3="hello world" |
| 2
console. log(f3.lastIndexOf('world')) //6 |
##3 |
console.log(f3.lastIndexOf('World')) / /-1 |
4 |
console.log(f3.lastIndexOf('hello')) //0 | | 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
var f5="hello world"
| 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. |
7:replace() – Used to find a string that matches a regular expression, and then replace the matching string with a new string.
8:search() – Perform a regular expression matching search. If the search is successful, the matching index value in the string is returned. Otherwise, -1 is returned.
9:slice() – Extract a part of the string and return a new string. (The parameter can be a negative number)
##1
var f6="hello world"
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. |
1
var f7="hello world";
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!