Home > Web Front-end > JS Tutorial > body text

JavaScript study notes (3) String Introduction to string type_Basic knowledge

WBOY
Release: 2016-05-16 17:52:35
Original
1074 people have browsed it

1. Character methods charAt(), charCodeAt(), fromCharCode()

Copy code The 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 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
Copy code Code 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", 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
Copy code The 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 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:
Copy code The code is as follows:

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
}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template