JavaScript Learning Notes (iii) string literal type Introduction _ Basics

Source: Internet
Author: User
Tags lowercase
1. Character Method Charat (), charCodeAt (), fromCharCode ()
Copy Code code 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 method Slice (), substr (), substring ()
Slice (), substring () method The first parameter specifies the starting position of the substring, the second parameter specifies the settlement position (excluding the end position), and the original string is unchanged
SUBSTR () The second parameter refers to the number of characters returned, the original string is 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 method IndexOf () and LastIndexOf ()
The IndexOf () method searches the substring from the back, receives a parameter or two parameters, the first parameter specifies the substring to search for, and the second parameter specifies to search backwards from that location, not to find the return-1
The LastIndexOf () method searches for a substring from the back and receives a parameter or two arguments, the first parameter specifies the substring to search for, and the second parameter specifies that a forward search from that location is not found, return-1
Copy Code code 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 capitalization conversion method toLowerCase () and toUpperCase ()
Convert toLowerCase () to lowercase, touppercase () to uppercase

5. Comparison of Strings Localecompare ()
Localecompare () can be compared in English, can also compare Chinese, uppercase letters in the first lowercase letter in the back

6. String sorting:
Copy Code code as follows:

var stringvalue= ["China", "Nan nan", "Jun Jun"];
Alert (Stringvalue.sort (Stringcompare));
//Ascending sort function-A-Z
function Stringcompare (value1,value2) {
return value1.localecompare (value2);//Descending z-a, Value1 and value2 interchange locations
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.