1. Character Methods: charAt (), charCodeAt (), fromCharCode ()
Copy codeThe Code is as follows:
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 the sub-string Methods slice (), substr (), and substring ()
The first parameter of slice () and substring () methods indicates the starting position of the stator string, and the second parameter specifies the settlement position (excluding the Ending position). The original string remains unchanged.
The second parameter of substr () indicates the number of returned characters. The original string remains unchanged.
Copy codeThe Code is as follows:
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", which takes the last three characters of the array
Alert (stringValue. slice (-3); // "rld", which takes the last three characters of the array
3. String Location Methods indexOf () and lastIndexOf ()
The indexOf () method searches for substrings from and to the backend. It can receive one or two parameters. The first parameter specifies the substrings to be searched, and the second parameter specifies to search back from this position, -1 not found
The lastIndexOf () method searches forward and backward for a substring. It can receive one or two parameters. The first parameter specifies the substring to be searched, and the second parameter specifies to search forward from this position, -1 not found
Copy codeThe Code is as follows:
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-insensitive conversion methods: toLowerCase () and toUpperCase ()
ToLowerCase () to lowercase, toUpperCase () to uppercase
5. Comparison of strings localeCompare ()
LocaleCompare () can be English or Chinese, with uppercase letters followed by lowercase letters
6. String sorting:
Copy codeThe Code is as follows:
Var stringValue = ["China", "Nan", "Jun"];
Alert (stringValue. sort (stringCompare ));
// Ascending sort function a-z
Function stringCompare (value1, value2 ){
Return value1.localeCompare (value2); // in descending order z-a, value1 and value2 interchange locations
}