Common methods of JS string

Posted by pinhead on Thu, 13 Jan 2022 09:05:27 +0100

Common methods of string

There are many methods of string, but there are only those commonly used. Some of them have not been used. Here is a brief introduction

1.charAt(index): returns the values specified in the following table. The returned value is a new string without affecting the original string

let str = 'hello';
let newStr = str.charAt(1);
console.log(str)      // hello
console.log(newStr)   // e

2.charCodeAt(index): returns the Unicode value of the values specified in the following table. The returned value is Unicode code and does not affect the original string

let str = 'hello';
let newStr = str.charCodeAt(1);
console.log(str)      // hello
console.log(newStr)   // 101

3. From charcode (U1, U2, U3): This is the static method used by String itself. The parameter is unicode code and returns the corresponding value

let str = String.fromCharCode(72,69,76,76,79);
let str1 = String.fromCharCode(97,98,99)
console.log(str)      // HELLO
console.log(str1)     // abc

4.concat(val1,val2,val3,val4...): Connect two or more strings without changing the existing string. The return value is the spliced string

let str = 'hello';
let newStr = str.concat('world','!','.....');
console.log(str)      // hello
console.log(newStr)   // helloworld!.....

5.indexOf(str, [start]): when the search string contains a value, find and return the index value of the first occurrence position. If not found, return - 1. Start is the start position of the search. The start position is included in the search range. It is optional. You can start the search from 0 by default.

let str = 'hello';
console.log(str.indexOf('el'))      // 1
console.log(str.indexOf('al'))      // -1
console.log(str.indexOf('el',2))    // -1

6.lastIndexOf(str, [start]): lastIndexOf is used in the same way as indexOf, except that it returns the last location index value. If it cannot be found, it returns - 1. Start is the location where the search starts. It is optional and can be left blank. By default, it starts from string Start with length-1, that is, from the back to the front.

let str = 'hello';
console.log(str.lastIndexOf('l'))      // 3
console.log(str.lastIndexOf('c'))      // -1
console.log(str.lastIndexOf('l',2))    // 2

7.replace(regexp|str, replacetext): used to replace some strings in the string with other strings, but only once, that is, if there are multiple strings in the string, but only the first matching value will be replaced. Another is to use regularity, which is to replace what conforms to regularity with what you want to replace. The original string will not be changed.

let str = ' hello world ';
let newStr1 = str.replace('l','a')
let newStr2 = str.replace(/\s*/g,'')
let newStr3 = str.replace(/^\s*|\s*$/g,'')
console.log(str)        // ' hello world '
console.log(newStr1)    // ' healo world '
console.log(newStr2)    // 'hello world', this is to remove all spaces
console.log(newStr3)    // 'hello world', this is to remove the spaces at both ends of the string

8.trim(): delete the spaces at both ends of the string.

let str = ' hello world ';
let newStr1 = str.trim()
console.log(str)        // ' hello world '
console.log(newStr1)    // 'healo world'

9.slice(start, [end]): intercepts the string and returns a new string without changing the original string. The new string starts at the start position and ends at the end, but does not include the end position

let str = 'hello world';
let newStr1 = str.slice(1)
let newStr2 = str.slice(1,3)
console.log(str)        // 'hello world'
console.log(newStr1)    // 'ello world'
console.log(newStr2)    // 'el'

10.substr(start, [length]): intercepts the string and returns a new string without changing the original string. The new string starts at the start position and intercepts the string of length. If not, it will be intercepted to the end by default

let str = 'hello world';
let newStr1 = str.substr(1)
let newStr2 = str.substr(1,3)
console.log(str)        // 'hello world'
console.log(newStr1)    // 'ello world'
console.log(newStr2)    // 'ell'

11.substring(start, [end]): intercepts the string and returns a new string without changing the original string. The new string starts at the start position and ends at the end, but does not include the end position

let str = 'hello world';
let newStr1 = str.substring(1)
let newStr2 = str.substring(1,3)
console.log(str)        // 'hello world'
console.log(newStr1)    // 'ello world'
console.log(newStr2)    // 'el'

12.split(separator, [limit]): divide the string into an array as specified. One parameter is the specified string or regular, and the second parameter is the maximum length of the array. Optional.

let str = 'helloworld';
let str1 = 'hello,world';
let newStr1 = str.split('')
let newStr2 = str1.split(',')
console.log(str)        // helloworld
console.log(newStr1)    // ['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']
console.log(newStr2)    // ['hello', 'world']

PS: String can be converted to array, and array can also be converted to string. The method is join()
let arr = ['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']
let str = arr.join("")
console.log(str)

13. Tolower case (STR): change all strings to lowercase, English

let str = 'HELLOWORLD';
let newStr1 = str.toLowerCase('')
console.log(str)        // HELLOWORLD
console.log(newStr1)    // helloworld

14.toUpperCase(str): change all strings to uppercase, English

let str = 'helloworld';
let newStr1 = str.toUpperCase('')
console.log(str)        // helloworld
console.log(newStr1)    // HELLOWORLD

15.repeat(number): repeat the string. Number must be a positive integer

let str = 'helloworld';
let newStr1 = str.repeat(3)
console.log(str)        // helloworld
console.log(newStr1)    // helloworldhelloworldhelloworld

16.endsWith(str): check whether the string ends with the specified string

let str = 'helloworld';
let newStr1 = str.endsWith('ld')
console.log(str)        // helloworld
console.log(newStr1)    // true

17.includes(str): check whether the string contains the specified string or character

let str = 'helloworld';
let newStr1 = str.includes('ld')
console.log(str)        // helloworld
console.log(newStr1)    // true

18.match(regexp): find whether the string contains matching items according to the regular or string, and return array or null

let str = 'helloworld';
let newStr1 = str.match('ld')
console.log(str)        // helloworld
console.log(newStr1)    // ['ld', index: 8, input: 'helloworldld', groups: undefined]

19.search(regexp): find whether there are matches according to the regular or string. If yes, the index is returned, but - 1 is not returned

let str = 'helloworld';
let newStr1 = str.search('ld')
console.log(str)        // helloworld
console.log(newStr1)    // 8

20.localeCompare(str): judge the sorting of each letter, and return a positive value in front of the current string, followed by a negative value, and the same will return 0

let str = 'helloworld';
let newStr1 = str.localeCompare('ad')
let newStr2 = str.localeCompare('helloworld')
let newStr3 = str.localeCompare('halloworld')
let newStr4 = str.localeCompare('id')
console.log(str)        // helloworld
console.log(newStr1)    // 1
console.log(newStr2)    // 0
console.log(newStr3)    // -1
console.log(newStr4)    // -1

Topics: Javascript