Methods commonly used string

Query operation

1.charAt () can pass a parameter index passed, returns the specified character position

eg:

STR = the let ' the I Love you ' 
the let Result = str.charAt ( . 5 ) 
the console.log (Result); // E 
the console.log (STR); // the I Love you do not affect the original string (query)

2.charCodeAt () Unicode encoding pass a parameter index passed, returns the specified character position

STR = the let ' the I Love you ' 
the let Result = str.charCodeAt ( . 5 ) 
the console.log (Result); // 101 
the console.log (STR); // the I Love you do not affect the original string (query)

3.concat (string1, string2, ..., stringX) string concatenation, for stitching together one or more strings, and returns the new string concatenation obtained

let str = 'I love you'
let str2=[' test',' try']
let result = str.concat(...str2)
console.log(result); // I love you test try
console.log(str); // I love you不影响原字符串

let str = 'I love you'
let str2 = 12345
let result = str.concat(' ',String(str2),' 3223222')
console.log(result); //Love you 12345 3223222 the I 
console.log (str); // the I Love you do not affect the original string

 

4.slice () to extract pieces of the string, and return the extracted part in a new string

  • slice (start, end) method may extract a portion of the string, and return a new string portion is extracted
  • Using the start (containing necessary) and end (not included, optional) parameters to specify the character string extraction portion
  • If it is negative, the predetermined parameter is counted from the end of the string to the start position. That is, the last character string refers -1, -2 means penultimate characters, and so on
  • STR = the let 'the I Love you' 
    the let str.slice Result = (0, -1 ) 
    the console.log (Result); // the I Love YO 
    the console.log (STR); // you do not affect the I Love original string 
    / / even a parameter that can be negative intercept is the second parameter does not contain

     

5.substr (start, length) substr ( ) method to extract the specified index from the start of the start of the string number of characters

Character 6.substring (from, to) substring () method for extracting a string intermediary between two specified index

position

1.indexOf (searchValue, Start)    the indexOf () method returns a string value of the specified position of the first occurrence of the string, if no matching string found returns -1

2.lastIndexOf (searchvalue, start) returns the position of a specified string value of the last occurrence of the second parameter if the designated start, specify a location in the search string from back to front of the string if a match is not found or -1

String case conversion method

1.toLowerCase () string.toLowerCase () method is used to convert strings to lower

2.toUpperCase string.toUpperCase () method is used to convert a string to upper case

The pattern string matching method

1.match () to retrieve the value in the specified string, or to find one or more regular expression matching syntax: string.match (regexp)

Returns: array. If no match is found return null

2.search () string.search (searchvalue), searchvalue is the search string or regular expression /// method returns the string matches the first index entry; if a match is not found, returns -1 (always Find mode is back from the beginning of the string)

3.replace () for replacement with other character string some characters, or alternatively a regular expression match the substring not alter the original string syntax: string.replace (searchvalue, newvalue)

let str = 'I love you'
let result = str.replace('I','www')
console.log(result); //www love you
console.log(str); // I love you不影响原字符串

4.split () method for dividing a character string into a string array

Syntax: string.split (separator, limit) does not change the original string

separator: Optional. String or a regular expression, which specifies the division stringObject place

limit: Optional. This parameter specifies the maximum length of the array returned. If this parameter is set, returns the substring than this parameter is not specified array. If this parameter is not set, the entire string will be split, without regard to its length

5.includes () determines whether a character string contained in another string

语法:string.includes(searchvalue, start)

Guess you like

Origin www.cnblogs.com/baixiaoxiao/p/11314403.html