Common JavaScript string method_javascript skills

Source: Internet
Author: User
Tags string methods
This article mainly introduces information about common methods of JavaScript strings. For more information, see 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 (); // ''if the length is only, you can find str. charAt () cannot be found, it is also a Null String '',~ Str. length-is a valid range. Alert (str. charAt (); // 'I' is not written by default. Find the first character alert (str. charAt (); // 'alert (''. charAt (); // alert (''. charAt (,); // alert (str. charCodeAt (); // unicode encoding alert (''. charCodeAt (); // alert (String. fromCharCode (,); // '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/'{alert (str. indexOf ('bai '); // if you find one from left to right, alert (str. indexOf ('M',) // locate alert (str. indexOf ('x') // If-does not exist, the result is-indicating that alert (str. lastIndexOf ('ww '));//

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 (-,); // 'I' alert (str. substring (); // string alert (str. substring (,); // 'I am' and str. substring (,) is the same. We can detect two numbers, but they are still large and small. Negative number. Alert (str. slice (,); // blank cannot be found. alert (str. slice (-); // 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 = ''; alert (typeof str); // stringalert (typeof str. split (); // objectalert (str. split (). length); // [''] alert (str. split (''); // ['', ''] alert (str. split ('A'); // [''] alert (str. split (''); // ['', ''] alert (str. split (''). length); // ['',''] alert (str. split (''); // ['', ''] //'' consists of five ''and four characters. split (''); // ['', ''] alert (str. split (''); // ['', ''] 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.

Javascript string connection class

When we write the front-end js, many strings are often spliced through "+" and then mounted to a DOM element. However, I will not compare the effect of using "+" to splice strings in different browsers. There are many such comparisons on the Internet. Many cool people say that using the join method of Array in js to concatenate strings is very good. To this end, write a js class in the project to uniformly process String concatenation.

Code

// A custom string connection class for concatenating strings, which improves the performance of function StringBuffer () {this. _ strs = new Array ();} StringBuffer. prototype. append = function (str) {this. _ strs. push (str) ;}; StringBuffer. prototype. arrayToString = function () {return this. _ strs. join ("");};

When using this class, we can directly use the following method:

var strBuff=new StringBuffer();strBuff.append("hello,");strBuff.append("Welcome to Javascript!");alert(strBuff.arrayToString());
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.