Javascript learning notes (3) String type Introduction

Source: Internet
Author: User
Tags string methods

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
}

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.