JavaScript string common classes use method summary

  • 2020-05-27 04:26:50
  • OfStack

For class:

1) dynamic method:

charAt: gets the character at the specified position in the string. (parameter: 1, specify the character position to get)

1, do not accept negative Numbers, if a negative number, will return an empty string.

2. If no parameter is given, the default is to get the character at position 0.

3, only one parameter is received.

charCodeAt: gets the Unicode encoding of the character at the specified position in the string (parameter: 1, specifying the character position to get the character encoding)

1, any character has a unique 1 character encoding.

2, only one parameter is received.

Commonly used:

Numbers: 48 to 57

Underline: 95

Space: 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. Multiple parameters can be received.

2. The writing method is fixed (static method) : String.fromCharCode (); // character encoding valid range: 0 ~ 65535 String is the object of the string


 var str = ' I'm a string ';
 alert( str.charAt( 2 ) );  //''   If the length is zero 5 , and found str.charAt(12)  Could not find also empty string '' . 0~str.length-1 It's legal. 
 alert( str.charAt( ) );   //' I '  The default is not 0 To find the first 1 A character 
 alert( str.charAt(2) );   // ' word '
 alert( '1234'.charAt( 2 ) ); //3
 alert( '1234'.charAt(2,3) ); //3
 alert( str.charCodeAt( 2 ) );//23383 unicode coding 
 alert( '1'.charCodeAt() );  // 49
 alert(String.fromCharCode(23383 ,21619)); //' The word taste '  Convert to characters according to encoding (static method) using multiple characters  ,  separated 

To find the class:

indexOf: looks for the first occurrence of the specified substring in a string. (the first parameter, specifying the substring to look for; The second parameter specifies where to start the lookup.

1. Look from front to back, starting from position 0 by default.

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

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

lastIndexOf: finds the last occurrence of a specified substring in a string. (the first parameter, specifying the substring to look for; The second parameter specifies where to start the lookup.

1. Search from the back and forward, starting from the length-1 position by default.

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


 var str = 'www.baidu.com/2015';
 alert(str.indexOf('bai'));  //4  Look from left to right 1 You go back and you don't look to the right anymore 
 alert(str.indexOf('m',5))  //12  From the first 5 Bit starts looking to the right 
 alert(str.indexOf('X'))   //-1  If it doesn't exist, it turns out to be  -1  Not found 
 alert(str.lastIndexOf('ww')); //1

Intercept class:

substring: extracts a 1-bit substring of the specified range. (the first parameter, specifying the starting location to extract; The second parameter, specifying the end location to extract.

1. The extraction range includes the starting position, but does not include the ending position.

2, the second parameter can be omitted to indicate the extraction from the starting position to the end of the string

3. The size of the two parameters will be compared before extraction, and then the position of the parameters will be adjusted from small to large, and then extracted.

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

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

slice: extracts a 1-bit substring of the specified range. (the first parameter, specifying the starting location to extract; The second parameter, specifying the end location to extract.

1. The extraction range includes the starting position, but does not include the ending position.

2, the second parameter can be omitted to indicate the extraction from the starting position to the end of the string

3. Do not compare the positions of two parameters, let alone adjust the positions

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

5, a negative number represents the position of a number of characters from the end of the string, and the position of the first character is -1.


 var str = ' I'm a string ';
 alert(str.substring());   //' I'm a string '
 alert(str.substring(-2,1)); //' I '
 alert(str.substring(2));  // string 
 alert(str.substring(0,2)); //' I am a '  with str.substring(2,0) is 1 Kind of. You can test two Numbers, the big one back and the small one back. As a negative number 0 To deal with. 
 alert(str.slice(2,0));    // blank   Can't find it. Don't switch places 
 alert(str.slice(-2));     //' String is '  Negative Numbers are backwards 

Compare class:

alert (' I ' > 'you'); //true string comparison compares the size of the Unicode value corresponding to the first character, and the following is not compared.

Other categories:

alert (str length); // gets the length of the string

split() // cuts strings into arrays

Parameter: 1 specifies 1 separator to split the string.

1, if you do not specify a separator, you will not split it, and you will simply store it in an array.

2. According to the separator, the values on the left and right sides of the separator are stored in an array.

3. The separator itself is not stored in the array.

4. The separator can only be a substring existing in the string.

5. In split's view, two characters must be concatenated with an empty string.

6. When you split an empty string with an empty string, you get an empty array.


var str = '1234';
alert( typeof str ); //string
alert( typeof str.split() ); //object
alert( str.split().length ); //['1234']
alert( str.split('2') ); //['1','34']
alert( str.split('a') ); //['1234']
alert( str.split('23') ); //['1','4']
alert( str.split('').length ); //['1','2','3','4']
alert( str.split('') );    //['1','2','3','4']
//'1234'  by 5 a  ''  add  4 Character composition 
alert( str.split('1234') );//['','']
alert( str.split('1') );  //['','234']
alert( ''.split(' ').length ); //['']
alert( ''.split('').length ); //[]
// Special case, only in this case  split  It returns an empty array. 

trim() : removes all Spaces from the beginning and end of a string. (the space in the middle of the string is kept).

html5 is a new method that is not supported by the lower version of the browser.

toUpperCase() : converts all strings to uppercase. (no parameters)

toLowerCase() : converts all strings to lowercase. (no parameters)

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

The above is the entire content of this article, I hope to be able to help you.


Related articles: