javascript string methods of learning summary

1, charAt (index): Returns the specified position in the character string

var STR = ' ABCDEFGHI ' ; 
the console.log (str.charAt ( 2 )); // output c

2, concat (): for connecting a plurality of strings

var str1 = 'hello ';
var str2 = 'world!';
console.log(str1.concat(str2)) // 输出 hello world!

3, indexOf (str, [m]): retrieve str subscript, m for start retrieving from what position in the original string, may be omitted

var str = 'hello world'
console.log(str.indexOf('el')) //输出1

4, match (str / Reg): retrieving a value specified in string str / Reg, and returns an array, comprising (a specified value, the specified value of the information in the index value in the original string, the original string, etc.)

var str = 'javascript001';
console.log(str.match('sc')); // 输出["sc", index: 4, input: "javascript001", groups: undefined]
console.log(str.match(/\d{2}/)); //输出["00", index: 10, input: "javascript001", groups: undefined]

5, replace (Reg / str, replacement): This method is used some of the characters (Replacement) replaced with other characters in the string, or alternatively a substring match the positive expression, does not change the original string

var str = 'javascript001';
console.log(str.replace('j','J')); // 输出 Javascript001
console.log(str.replace(/\d{1}/,'G')) //输出javascriptG01

6, search (): To find the character search, index returns 

var STR = ' javascript001 ' ; 
the console.log (str.search ( ' S ' )) // output. 4 
the console.log (str.search (/ \ D { . 1 } /)) // Output 10

7, slice (start, end): extracting a portion of the string, and return a new string portion is extracted, start at start extracting the representative table, representative of the end of the extraction end under the table, does not contain end, not change the original string

var str = 'javascript001';
console.log(str.slice(4,10)); //输出script

8, split (sep): a method for dividing a character string into a string array. Partitioned from the parameter (On Sep) specified place, without changing the original character string

var STR = ' javascript001 ' ; 
the console.log (str.split ( ' S ' )) // Returns [ 'java', 'cript001' ], s is treated as a delimiter.

9, substr (start, [length]): extracting a target string in the string, start index representative of the start, length representative of the extracted length, to extract the last representatives omitted, without changing the original character string

var STR = ' javascript001 ' ; 
the console.log (str.substr ( . 1 , . 5 )) // output avasc 
the console.log (str.substr ( . 4 )) // output script001

10, substring (start, [end]): This method is used to extract the string of characters between two intermediary specified index. start can not be negative, representing the start of the next start extracting table, end table at the end represents the extract, does not contain end. And substr () confusing, the difference in the second parameter passed

var str = 'javascript001';
console.log(str.substring(0,4)) //输出lava
console.log(str.substring(4)) //输出script001

11, includes ( 'x', [m]): returns a Boolean value that indicates whether the parameter x in the original string string, m for start search

var str = 'javascript001';
console.log(str.includes('java')); // true

12, startsWith (): returns a Boolean value indicating whether the parameter string at the head of the original string

13, endsWith (): returns a Boolean value indicating whether the parameter string of the string at the end of the original

14, repeat (n): represents the original string repeated n times, the new string returned after the repetition

var STR = ' the JS ' ; 
the console.log (str.repeat ( . 3 )); // output JSJSJS

 

Reprinted: https://segmentfault.com/a/1190000014799376

Guess you like

Origin www.cnblogs.com/zhuzhaoli/p/11130751.html