1. Character methods charAt(), charCodeAt(), fromCharCode()
var stringValue = "hello world";
alert(stringValue.charAt(1)); //"e"
alert(stringValue[1]); //"e"
alert( stringValue.charCodeAt(1)); //101
alert(String.fromCharCode(104,101)); //"he"
2. Return substring methods slice(), substr (), substring()
The first parameter of the slice(), substring() method specifies the starting position of the substring, and the second parameter specifies the settlement position (excluding the end position), and the original string remains unchanged
The second parameter of substr() refers to the number of characters returned, the original string remains unchanged
var stringValue = "hello world";
alert(stringValue.slice(3)); //"lo world"
alert(stringValue.substring(3)) ; //"lo world"
alert(stringValue.substr(3)); //"lo world"
alert(stringValue.slice(3,7)); //"lo w"
alert(stringValue.subtring(3,7)); //"lo w"
alert(stringValue.substr(3,7)); //"lo worl"
alert(stringValue.slice (-3)); //"rld", take the last 3 characters of the array
alert(stringValue.slice(-3)); //"rld", take the last 3 characters of the array
3. String position methods indexOf() and lastIndexOf()
The indexOf() method searches substrings from front to back. It can receive one parameter or two parameters. The first parameter specifies the substring to be searched. , the second parameter specifies to search backward from this position, if not found, it returns -1
lastIndexOf() method searches the substring from back to forward, and can receive one parameter or two parameters, the first parameter specifies the value to be searched Substring, the second parameter specifies to search forward from this position, if not found, return -1
var stringValue = "hello world";
alert(stringValue.indexOf("o")); //4
alert(stringValue.lastIndexOf("o")); //7
alert(stringValue.indexOf("o",6)); //7
alert(stringValue.lastIndexOf("o",6)); //4
4. String case conversion methods toLowerCase() and toUpperCase()
toLowerCase() converts to lowercase, toUpperCase() converts to uppercase
5. String comparison localeCompare()
localeCompare() can compare English or Chinese, with uppercase letters in front and lowercase letters in the back
6. String sorting:
var stringValue= ["China","Nannan","Junjun"];
alert(stringValue.sort(stringCompare ));
//Ascending sorting function a-z
function stringCompare(value1,value2) {
return value1.localeCompare(value2); //Descending order z-a, value1 and value2 exchange positions
}