JavaScript provides a lot of useful apis for using string as a basic type. In this installment, we’ll summarize some common string methods

The method of string can be divided into four parts: adding, deleting, modifying and checking

Write in the front: to understand a method, we can through three aspects, 1. Parameter 3. Return value

All string methods described in this article create a new string without changing the original string

Concatenation or interception of strings

Str.concat () : Used to concatenate one or more strings, returning the new concatenated string

Parameter: There may be more than one string to concatenate to STR

let str = 'hello';
console.log(str.concat(' '.'world')) //'hello world'
Copy the code

Note: This method is not very efficient, it is better to use the “+” or ‘ ‘template string instead

2. Str.slice () : This method extracts a string and returns a new string

Parameters: 1)beginIndex: begins to extract the characters (including) of the string from the index. If the value is negative, the calculation starts from the last

2)endIndex: indicates that the string is extracted from the end of the index (excluding). If the index is omitted, the string is extracted until the end of the string. If the index is negative, the calculation starts from the end

let str = 'hello world';
console.log(str.slice(6)) //'world'
console.log(str.slice(-5, -3)) //'wo'

Copy the code

3. Str.substring () : This method does the same thing as the slice method: it extracts a string and returns the extracted string

Parameters: 1)startIndex, which indicates the index from which to extract the characters of the string (including)

2)endIndex, which means extracting strings from the end of the index (excluding)

If startIndex is greater than endIndex, substring is executed as if the two parameters were swapped. If startIndex is greater than endIndex, substring is executed as if the two parameters were swapped

let str = 'hello world';
console.log(str.substring(-1.5))  //'hello'
console.log(str.substring(5, -1))  //'hello'
Copy the code

Delete the string

Str.trim () : removes the whitespace characters at both ends of a string and returns the deleted new string without changing the original string

Change the string

1.str.tolowercase () : this method takes no arguments and returns the string value of the call toLowerCase

2. Str.touppercase () : This method takes no arguments, capitalizes the string value of the call and returns it

3. Str.replace () : You can replace part of a string with a replacement value, returning the new replaced string

Arguments: 1) The substring or regular expression in a string to be replaced. The default value replaces the first one. You can set the global pattern in the regular expression to replace all matching substrings

2) A replacement value

let str = 'hello world';
console.log(str.replace(/o/g."f")) //"hellf wfrld"
Copy the code

4. Str.split () : Can use a specified delimiter to split the string into an array, returning an array

Arguments: 1) A delimiter, which can be either a string or a regular expression. If empty, it splits each character. Default global split

2) The length of the split (optional), which is an integer limiting the number of splits. If this number is exceeded, the rest of the text in the new array is not returned

let str = 'hello world';
console.log(str.split("")) //["hello", "world"]
Copy the code

4. Query string

1. Str.charat () : Returns the specified character from a string

Parameter: index, an integer between 0 and length-1. The default value is 0

let str = 'hello world';
console.log(str.charAt(1)) //'e'
Copy the code

Str.includes () : Determines whether the string contains the specified characters. If yes, return true. If no, return false

Parameter: subStr, the specified string

let str = 'hello world';
console.log(str.includes('hello')) //true
console.log(str.includes('fire')) //flase
Copy the code

3. Str.indexof () : determines whether the string contains the specified character. If yes, return the position of the indexOf the character (immediately returned if found)

Parameter: subStr, the specified string

let str = 'hello world';
console.log(str.indexOf('world')) / / 6
console.log(str.indexOf('fire')) / / 1
Copy the code

4. Str.lastindexof () : The usage is basically the same as indexOf, except that lastIndexOf() searches backwards

5. Str.search () : Searches for the specified string using a regular expression, returning the index of the first successful match if found, or -1 if not found

Parameter: a regular expression that is implicitly converted to a regular expression object if passed a non-regular expression

let str = 'hello world';
console.log(str.search('world')) / / 6
console.log(str.search(/w/)) / / 6
Copy the code

6. Str.match () : Returns the result of a string matching a regular expression. If no global match is set, the first complete match and the associated capture group are returned.

Parameter: a regular expression that is implicitly converted to a regular expression object if passed a non-regular expression

let str = 'hello world';
console.log(str.match(/l/)) //["l", index: 2, input: "hello world", groups: undefined]
console.log(str.match(/l/g)) //["l", "l", "l"]
Copy the code

Well, that’s all for this episode, if it helped you, please give it a like!!