Common use methods of JavaScript strings _ javascript tips-js tutorial

Source: Internet
Author: User
Tags string methods
This article shares examples and brief descriptions of some of the most common and useful string-related methods summarized over the past few years. It is easy for programmers to use for quick reference. Of course, the most experienced developers are familiar with these operations, but I think this is a good method to help beginners understand these functions. It can help you use simple syntax, complete complex operations. Retrieve class:

1) dynamic method:

CharAt: gets the character at the specified position of the string. (Parameter: Specifies the character location to be obtained)

1. Negative numbers are not accepted. If it is a negative number, an empty string is returned.

2. If no parameter is provided, the default value is to obtain the character at the 0th position.

3. Only one parameter is received.

CharCodeAt: Get the Unicode encoding of the character at the specified position in the string (parameter: one, specify the character location to get the character encoding)

1. Any character has a unique character encoding.

2. Only one parameter is received.

Frequently used:

Number: 48 ~ 57

Underline: 95

Space: 32

Tab: 9

Lowercase letter: 97 ~ 122

Uppercase letters: 65 ~ 90

2) static method:

FromCharCode: returns the corresponding character based on the specified character encoding. (Parameter: any number of parameters)

1. You can receive multiple parameters.

2. The statement is fixed (static): String. fromCharCode (); // valid range of character encoding: 0 ~ 65535 String is a String object

Var str = 'I am string'; alert (str. charAt (2); // ''if the length is only 5, you can find str. charAt (12) cannot be found, which is also a Null String '', 0 ~ Str. length-1 is a valid range. Alert (str. charAt (); // 'I' is not set to 0 by default. Find the first character alert (str. charAt (2); // 'alert ('123 '. charAt (2); // 3 alert ('20140901 '. charAt (2, 3); // 3 alert (str. charCodeAt (2); // 23383 unicode encoding alert ('1 '. charCodeAt (); // 49 alert (String. fromCharCode (23383,216 19); // 'wordw' is separated

Search Class:

IndexOf: searches for the position of the specified substring in the string for the first time. (The first parameter specifies the substring to be searched, and the second parameter specifies the position to start searching .)

1. Search from the past and later. The default value starts from the 0th position.

2. If it is found, the location found for the first time is returned. If it is not found,-1 is returned.

3. If the second parameter is negative, it is treated as 0 by default.

LastIndexOf: locate the last occurrence of the specified substring in the string. (The first parameter specifies the substring to be searched, and the second parameter specifies the position to start searching .)

1. Search from the back to start. The default value is from the length-1 position.

2. If it is found, the location found for the first time is returned. If it is not found,-1 is returned.

Var str = 'www .baidu.com/2015'; alert (str. indexOf ('bai '); // 4 find one from left to right, and then the system will not find alert (str. indexOf ('M', 5) // 12 search for alert (str. indexOf ('x') // If-1 does not exist, the result is-1 indicating that alert (str. lastIndexOf ('ww '); // 1

Truncation class:

Substring: extract a substring from a specified range. (The first parameter specifies the start position to be extracted; the second parameter specifies the end position to be extracted .)

1. The extraction range includes the start position, but not the end position.

2. The second parameter can be omitted, indicating that the string is extracted from the starting position to the end of the string.

3. before extraction, the size of the two parameters will be compared, and the parameter location will be adjusted in the ascending order, and then extracted.

4. All invalid parameters are automatically converted to 0.

5. If no parameter is provided, the entire string is directly returned by default.

Slice: extract a substring from a specified range. (The first parameter specifies the start position to be extracted; the second parameter specifies the end position to be extracted .)

1. The extraction range includes the start position, but not the end position.

2. The second parameter can be omitted, indicating that the string is extracted from the starting position to the end of the string.

3. the positions of the two parameters are not compared, and the positions are not adjusted.

4. parameters can be positive or negative. All other invalid parameters are converted to 0.

5. A negative number indicates the position of the forward digit from the end of the string. The position of the most character is-1.

Var str = 'I am string'; alert (str. substring (); // 'I am the string' alert (str. substring (-2, 1); // 'I' alert (str. substring (2); // string alert (str. substring (); // 'I am' and str. substring (2, 0) is the same. We can detect two numbers, but they are still large and small. The negative number is treated as 0. Alert (str. slice (2, 0); // blank cannot be found, no switch location alert (str. slice (-2); // The 'string' negative number is the forward number from the back

Comparison class:

Alert ('I'> 'you'); // If the true string is compared, the Unicode value corresponding to the first character is not compared.

Other types:

Alert (str. length); // obtain the string length

Split () // cut the string into Arrays

Parameter: Specifies a delimiter to separate strings.

1. If the Delimiter is not specified, it is not split and saved to the array directly.

2. Store the values of the left and right sides of the delimiter into an Array Based on the delimiter.

3. The delimiter itself is not saved to the array.

4. The delimiter can only be a substring that exists in a string.

5. In split's view, the two characters must be connected by a null string.

6. When an empty string is used to separate an empty string, an empty array is obtained.

Var str = '000000'; alert (typeof str); // stringalert (typeof str. split (); // objectalert (str. split (). length); // ['20140901'] alert (str. split ('2'); // ['1', '34'] alert (str. split ('A'); // ['20140901'] alert (str. split ('23'); // ['1', '4'] alert (str. split (''). length); // ['1', '2', '3', '4'] alert (str. split (''); // ['1', '2', '3 ', '4'] // '123' consists of five ''' and four characters. alert (str. split ('20140901'); // ['',''] alert (str. split ( '1'); // ['', '000000'] alert (''. split (''). length); // [''] alert (''. split (''). length); // [] // special case. Only in this case can split return an empty array.

Trim (): removes all spaces at the beginning and end of a string. (Spaces in the middle of the string are retained ).

The new html5 method is not supported by browsers of earlier versions.

ToUpperCase (): converts all strings to uppercase. (No parameters)

ToLowerCase (): converts all strings to lowercase letters. (No parameters)

Finally, all string methods do not modify the string itself.

The above is all the content of this article, hoping to help you.

Related Article

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.