JavaScript Common string methods

Source: Internet
Author: User
Tags string methods

Get class :

1) Dynamic Method:

charAt : Gets the character at the specified position of the string. (parameter: one that specifies the character position to get)

1, negative numbers are not accepted, and if negative, an empty string is returned.

2, if the parameter is not given, the default is to get the No. 0 character on the position.

3, only one parameter is received.

charCodeAt : Gets the Unicode encoding of the character at the specified position in the string (parameter: one that specifies the character position to get the character encoding)

1, any character has a unique character encoding.

2, only one parameter is received.

Common:

Numbers: 48 ~ 57

Underline: 95

Spaces: 32

Tab: 9

Lowercase letters: 97 ~ 122

Capital letters: 65 ~ 90

2) static method:

fromCharCode : Returns the corresponding character according to the specified character encoding. (parameter: any number)

1, you can receive multiple parameters.

2, the notation is fixed (static method): String.fromCharCode (); Character encoding valid range: 0 ~ 65535 string is an object

1 varstr = ' I am a string ';2Alert (Str.charat (2));//"If the length is only 5, but find Str.charat (12) can not find is also an empty string", 0~str.length-1 is the legal range. 3Alert (Str.charat ());//' i ' default does not write is 0, find the first character4Alert (Str.charat (2));//' word '5Alert (' 1234 '. CharAt (2));//36Alert (' 1234 '. CharAt (2,3));//37Alert (Str.charcodeat (2));//23383 Unicode encoding8Alert (' 1 '. charCodeAt ());// the9Alert (String.fromCharCode (23383, 21619));//' word taste ' is converted into characters according to encoding (static method) multiple words trailing characters, separated

find a class :

indexOf : Finds the position of the specified substring in the first occurrence of the string. (the first argument, which specifies the substring to find, and the second parameter specifies where to start the lookup.) )

1, from the previous lookup, the default starting from the No. 0 place.

2, if found, return to the first found location, if not found, return-1.

3, if the second parameter is a negative number, the default is zero

lastIndexOf : Finds the location of the last occurrence of the specified substring in the string. (the first argument, which specifies the substring to find, and the second parameter specifies where to start the lookup.) )

1, from the back to look forward, the default starting from the length-1 position.

2, if found, return to the first found location, if not found, return-1.

1 var str = ' www.baidu.com/2015 '; 2 alert (str.indexof (' Bai '));   // 4 Find  one from left to right and return to the right 3 alert (str.indexof (' m ', 5))    // 12 starting from 5th position, right . 4 alert (str.indexof (' X '))      //-1 does not exist The result is-1 means no 5// 1

Intercept class :

substring : Extracts a substring of a specified range. (The first argument, which specifies the starting position to extract , and the second argument, which specifies the end position to extract.) )

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

2, you can omit the second argument, which means extracting from the start position to the end of the string

3, before the extraction will compare the size of two parameters, and then adjust the parameter position in order from small to large, and then extract.

4, all illegal parameters will be automatically converted to 0.

5, if the parameter is not given, the entire string is returned by default.

Slice : Extracts a substring of a specified range. (The first argument, which specifies the starting position to extract , and the second argument, which specifies the end position to extract.) )

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

2, you can omit the second argument, which means extracting from the start position to the end of the string

3, does not compare the position of the two parameters, and does not adjust the location of the position

4, the parameter can be positive or negative, and all other illegal parameters will be converted to 0.

5, a negative number indicates the position of the digit character from the back of the string, and the position of the most characters is-1.

1 varstr = ' I am a string ';2Alert (str.substring ());//' I'm a string '3Alert (str.substring ( -2,1));//' Me '4Alert (str.substring (2));//string5Alert (str.substring (0,2));//' I am ' is the same as str.substring (2,0). Can detect two number, big back still, small forward still. Negative numbers are treated as zero. 6Alert (Str.slice (2,0));//blank cannot be found, no swap position7Alert (Str.slice (-2));//The ' character string ' negative number is backwards from behind.

Comparison class :

Alert (' I '>' you '); True string comparisons compare the size of the Unicode value corresponding to the first character and are not compared in the rear.

Other Classes :

Alert (str.length); Get string length

Split ()//To cut a string into an array

Parameter: one specifies a delimiter to use to split the string.

1, if you do not specify a separator, it will not be split, directly into the array.

2, the values of the left and right sides of the delimiter are deposited into an array based on the delimiter.

3, the delimiter itself is not stored in an array.

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

5, in Split view, two characters must be connected by an empty string.

6, when an empty string is used to split an empty string, an empty array is obtained.

1 varstr = ' 1234 ';2AlerttypeofSTR);//string3AlerttypeofStr.split ());//Object4Alert (Str.split (). length);//[' 1234 ']5Alert (Str.split (' 2 '));//[' 1 ', ' ""]6Alert (Str.split (' a '));//[' 1234 ']7Alert (Str.split (' 23 '));//[' 1 ', ' 4 ']8Alert (Str.split ("). length);//[' 1 ', ' 2 ', ' 3 ', ' 4 ']9Alert (Str.split ("));//[' 1 ', ' 2 ', ' 3 ', ' 4 ']Ten //' 1234 ' consists of five ' plus four characters OneAlert (str.split (' 1234 '));//[', '] AAlert (Str.split (' 1 '));//[' ', ' 234 '] -Alert ('. Split ('). length);//["] -Alert ('. Split ('). length);//[] the //exception, Split returns an empty array only in this case. 

trim () : Removes all whitespace from the end of the string. (spaces in the middle of the string are preserved).

HTML5 new method, low version browser is not supported.

touppercase () : Converts all strings to uppercase. (No parameters)

toLowerCase () : Converts all strings to lowercase. (No parameters)

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

JavaScript Common string methods

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.