JavaScript string common method

  • 2021-01-25 07:01:20
  • OfStack

For class:

1) Dynamic method:

charAt: Gets the character at the specified position in the string. (Parameters: 1, specifying the position of the character to be fetched)

1, do not accept negative numbers, if negative, will return an empty string.

2, If no argument is given, the default is to get the character in position 0.

3. Only 1 parameter is received.

charCodeAt: Gets the Unicode encoding of the character at the specified position in the string (parameter: 1, specifying the character position at which the character encoding is to be obtained)

1, any character has a 1 only character encoding.

2, only receive 1 parameter.

Commonly used:

Numbers: 48 to 57

Underline: 95

Space: 32

Tab: 9

Lowercase letters: 97 ~ 122

Capital letters: 65 ~ 90

2) Static methods:

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

1, can receive multiple parameters.

String.fromCharCode(); String.fromCharCode(); String.fromCharCode(); // Valid range of character encoding: 0 ~ 65535


var str = ' I'm a string ';
alert( str.charAt( ) ); //''  If the length itself is only one, find str.charAt()  Cannot find an empty string '' . ~str.length- It's legal. 
alert( str.charAt( ) ); //' I '  Default not to write is, find the number 1 A character 
alert( str.charAt() ); // ' word '
alert( ''.charAt( ) ); //
alert( ''.charAt(,) ); //
alert( str.charCodeAt( ) );// unicode coding 
alert( ''.charCodeAt() ); // 
alert(String.fromCharCode( ,)); //' The word taste '  Convert multiple characters according to the encoding (static method) used  ,  separated  

To find the class:

indexOf: Finds the position where the specified substring first occurs in the string. (The first argument specifies the substring to look for; The second parameter specifies where to start the search.

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

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

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

lastIndexOf: Finds the position of the last occurrence of the specified substring in the string. (The first argument specifies the substring to look for; The second parameter specifies where to start the search.

1. Search from back to front, starting at position length-1 by default.

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


var str = 'www.baidu.com/';
alert(str.indexOf('bai')); //  Find it from left to right 1 I'm not going to go to the right anymore 
alert(str.indexOf('m',)) //  I'm going to start at the first place and go to the right 
alert(str.indexOf('X')) //-  If it doesn't exist, it turns out to be  -  I didn't find it. 
alert(str.lastIndexOf('ww')); // 

Intercept class:

substring: Extracts 1 substring from the specified range. (The first parameter specifies the starting position of the extraction; Parameter 2, specifying the end position to extract.

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

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

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

4. All illegal arguments are automatically converted to 0.

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

slice: Extracts 1 substring from the specified range. (The first parameter specifies the starting position of the extraction; Parameter 2, specifying the end position to extract.

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

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

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

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

5, a negative number means to count characters from the end of the string forward, the first character position is -1.


var str = ' I'm a string ';
alert(str.substring()); //' I'm a string '
alert(str.substring(-,)); //' I '
alert(str.substring()); // string 
alert(str.substring(,)); //' I am a '  with str.substring(,) is 1 Kind of. You can detect two numbers, the big one going back and the small one going forward. Let's deal with negative numbers. 
alert(str.slice(,)); // blank   If you can't find it, don't switch places 
alert(str.slice(-)); //' String is '  A negative number is going backwards  

Compare class:

alert (' I ' > 'you'); // The true string is compared to the Unicode value of the first character. The following characters are not compared.

Other categories:

alert(str.length); // Gets the length of the string

split() // Cut the string into an array

Parameter: 1 Specifies 1 separator used to split a string.

1, If you do not specify a divider, it will not be split and stored directly in the array.

2, store the left and right values of the divider into an array according to the divider.

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

4. A separator can only be a substring that exists in a string.

5. According to split, two characters must be concatenated by an empty string.

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


var str = '';
alert( typeof str ); //string
alert( typeof str.split() ); //object
alert( str.split().length ); //['']
alert( str.split('') ); //['','']
alert( str.split('a') ); //['']
alert( str.split('') ); //['','']
alert( str.split('').length ); //['','','','']
alert( str.split('') ); //['','','','']
//''  by 5 a  ''  add  4 Is composed of three characters. 
alert( str.split('') );//['','']
alert( str.split('') ); //['','']
alert( ''.split(' ').length ); //['']
alert( ''.split('').length ); //[]
// Exceptions. Only in this case  split  It returns an empty array.  

trim() : Remove all whitespace at the beginning and end of the string. (Spaces in the middle of the string are preserved.)

html5 new method, lower version browsers do not support.

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

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

And finally, none of the string methods will modify the string itself.

javascript string concatenation class

When we write the front-end js, it is common to have a number of strings concatenated with a "+" and then mount an DOM element. However, I won't compare the results of using "+" to concatenate strings in various browsers, as there are plenty of comparisons online. A lot of people say that it works well to concatenate strings using the join method in Array. For this purpose, I write an js class in the project, which is used to unify the string to handle concatenation.

code


// The custom of 1 String concatenation class, used to concatenate strings, than "+" To improve performance function StringBuffer()
{ 
this._strs = new Array(); 
}StringBuffer.prototype.append = function(str)
{ 
this._strs.push(str); 
};StringBuffer.prototype.arrayToString = function() 
{ 
return this._strs.join(""); 
}; 

When we use 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 articles: