Common methods of string in JS

Source: Internet
Author: User

1.CHARAT returns the specified index out of the character Var str= ' ABCD ';  var a=str.charat (0); Console.log (a); ' A ' console.log (str); ' ABCD ' 2. charCodeAt returns the Unicode character of the specified index

Str.charcodeat (0); 97

3.indexof determines the first occurrence of a character in the index of a string if it contains an index that returns it if it does not contain return-1.

Str.indexof (' a ');     0 str.indexof (' e '); -1

4.lastIndexOf determines the index of the last occurrence of a character in a string if it contains an index that returns it if it does not contain return-1.

Str.lastindexof (' B ');   1 str.lastindexof (' e '); -1 5.concat Stitching 2 strings, returning a new string, without any change to the original string.

var str= ' qwe ';     var str1= ' abc '; var str2=str.concat (STR1);

Console.log (STR2);//"QWEABC"

6.SUBSTR (n,m) starts with index n, intercepts m characters, returns the intercepted characters, and does not change the original string.  var b=s.substr (Console.log) (b); ' W '

7.substring (n,m) from index n, intercept to index m, excluding m. Returns the intercepted characters without any change to the original string.

var ee=str.substring (1,3);  Console.log (EE); "BC"

8.slice (n,m) from index n, intercept to index m, excluding m. Returns the intercepted characters without any change to the original string.   var aa=str.slice (0,3); Console.log (AA);//' abc '

9.split splits a string with the specified character, returning an array. There is no change to the original string.

var a=str.split (');  Console.log (a); ["A", "B", "C", "D"]

10.replace (' A ', 1); Replaces the specified character, returns a new string after substitution, and changes the original string. (the first parameter can be a regular expression)   can only be replaced once, with the regular pattern modifier g using var str= ' Aaaaee ';   var reg=/a/g;   Str.replace (reg,1); "1111ee" 11.match () retrieves the specified value within a string, or finds a match for one or more regular expressions. Put the found character in the array and return an array.

var str= ' aaaa3ed33 ';   var reg=/a/g;  Str.match (REG); ["A", "a", "a", "a"]

The 12.search () method is used to retrieve a substring specified in a string, or to retrieve a substring that matches a regular expression.

Es6 new methods of increase

1.codePointAt () is capable of correctly handling 4 bytes of stored characters, returning a code point that specifies the index of one character. The Codepointat method is the simplest way to test whether a character is made up of two bytes or four bytes.

 var s = ‘??a‘;

 scodepointat (0 //134071

 s.codePointAt(1) // 57271

codePointAtmethod returns the decimal value of the code point, and if you want a hexadecimal value, you can use the toString method to convert it.

var s=;s. Codepointat (0tostring (16//"20bb7" S.codepointat2 . Tostring (16//"61" does not recognize the 32-bit UTF-16 character          

for...ofIt is used in conjunction with loops because it correctly recognizes 32-bit UTF-16 characters.

var s=‘?? Afor  (let ch of S{console.log. (0tostring (16;} //20bb7//        

2.codePointAt is used to return the corresponding character from the code point, but this method does not recognize the 32-bit UTF-16 character (the Unicode number is greater than 0xFFFF ).

String.fromCodePoint(0x20BB7)// "??"String.fromCodePoint(0x78, 0x1f680, 0x79) === ‘x\uD83D\uDE80y‘// true
3.for.. The traversal interface of the string
for (let codePoint of ‘foo‘) { console.log(codePoint)}// "f"// "o"// "o"

4.at () returns the character at the specified index

‘abc‘.at(0) // "a"‘??‘.at(0) // "??"

< Span class= "token punctuation" > < Span class= "token punctuation" > 5. Includes () : Returns a Boolean value that indicates whether the parameter string was found. All three methods support the second parameter, which indicates where to start the search.

6.startsWith (): Returns a Boolean value that indicates whether the parameter string is in the header of the source string. All three methods support the second parameter, which indicates where to start the search.

7.endsWith (): Returns a Boolean value that indicates whether the parameter string is at the end of the source string. All three methods support the second parameter, which indicates where to start the search.

var s=' Hello world! '; sstartswith (6) //Trues. Endswith ( ' Hello ' 5) //Truesincludes ( ' Hello ' 6)             

The 8.repeat () method returns a new string indicating that the original string is repeated n once.

‘x‘.repeat(3) // "xxx"‘hello‘.repeat(2) // "hellohello"‘na‘.repeat(0) // ""

9.padStart (), Padend () string complement full length function

Padstart () for head completion

Padend () for tail completion

padStartAnd a total padEnd of two parameters, the first parameter is used to specify the minimum length of the string, and the second argument is the string to complement.

' X '.Padstart(5,' AB ')' ABABX '' X '.padstart (4  ' ab ' ) //' Abax '  ' x ' .padend (5  ' ab ' ) //' Xabab '  ' x ' . Padend (4 ' ab ' ) //' Xaba '      

If the length of the original string is equal to or greater than the specified minimum length, the original string is returned.

‘xxx‘.padStart(2, ‘ab‘) // ‘xxx‘‘xxx‘.padEnd(2, ‘ab‘) // ‘xxx‘

If the string is used to complement the original string, and the length of the sum exceeds the specified minimum length, the complement string that exceeds the number of digits is truncated.

‘abc‘.padStart(10, ‘0123456789‘)

If you omit the second argument, the space complement length is used by default.

‘x‘.padStart(4) // ‘ x‘‘x‘.padEnd(4) // ‘x ‘

Common methods of string in JS

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.