编程知识 cdmana.com

Common methods of collating JavaScript strings

string Common ways to sort out

1.concat(), Method to join and merge one or more strings with the original string , Form a new string and return .

let a = "abc";
let b = a.concat(1, undefined, null, NaN, {});
console.log(a); // 'abc'
console.log(b); // 'abc1undefinednullNaN[object Object]'

2.replace(),replace() Method returns a replacement value (replacement) Replace some or all of the patterns (pattern) The new string after the match . A pattern can be a string or a regular expression , The replacement value can be a string or a callback function to be called every time a match is made . If pattern Is string , Replace only the first match .

let a = "abc abc abc";
let b = a.replace("abc", "ab");
console.log(a); // 'abc abc abc'
console.log(b); // 'ab abc abc'

3.toLowerCase(), The string value that calls the method is converted to lowercase , And back to .

let a = "ABC abc abc";
let b = a.toLowerCase();
console.log(a); // 'ABC abc abc'
console.log(b); // 'abc abc abc'

4.toUpperCase(), The string value that calls the method is converted to uppercase , And back to .

let a = "ABC abc abc";
let b = a.toUpperCase();
console.log(a); // 'ABC abc abc'
console.log(b); // 'ABC ABC ABC'

5.split(), split , Method uses the specified separator string to set a String Object is divided into substring arrays , The position of each split is determined by a specified split string .

let a = "ABC abc abc 123";
let b = a.split(" ");
console.log(a); // 'ABC abc abc 123'
console.log(b); // ["ABC", "abc", "abc", "123"]

6.trim(), Remove str White space characters at the beginning and end , return str A copy of , Does not affect the value of the string itself

let a = " a bc ";
let b = a.trim();
console.log(a); // ' a bc '
console.log(b); // 'a bc'

7.chatAt(), Method returns the specified character from a string .

let a = "ABC abc abc 123";
let b = a.charAt(1);
console.log(a); // 'ABC abc abc 123'
console.log(b); // 'B'

8.slice(), Method to extract part of a string , And return a new string , And it doesn't change the original string .

let a = "ABC abc abc 123";
let b = a.slice(2, -1);
console.log(a); // 'ABC abc abc 123'
console.log(b); // 'C abc abc 12'

9.trim(), Remove str White space characters at the beginning and end , return str A copy of , Does not affect the value of the string itself

let a = " a bc ";
let b = a.trim();
console.log(a); // ' a bc '
console.log(b); // 'a bc'

10.includes(), Method is used to determine whether a string is contained in another string , Return according to the situation true or false.

let a = "ABC abc abc 123";
let b = a.includes("abc", 1);
console.log(a); // 'ABC abc abc 123'
console.log(b); // true

11.indexOf(), Method returns the... That called it String The index of the specified value for the first time in an object , from fromIndex Search for . If the value is not found , Then return to -1.

let a = "ABC abc abc 123";
let b = a.indexOf("abc", 5);
console.log(a); // 'ABC abc abc 123'
console.log(b); // 8

12.lastIndexOf(), Method returns the... That called it String The index of the last occurrence of the specified value in the object , from fromIndex Search for . If the value is not found , Then return to -1. 'abab'.lastIndexOf('ab', 2) Will return 2 instead of 0, because fromIndex Only the beginning of the string to be matched .

let a = "ABC abc abc 123";
let b = a.lastIndexOf("abc", 8);
console.log(a); // 'ABC abc abc 123'
console.log(b); // 8

13.substr(), Method returns the number of characters in a string from the specified position to the specified number of characters .

let a = "ABC abc abc 123";
let b = a.substr(4, 7);
console.log(a); // 'ABC abc abc 123'
console.log(b); // 'abc abc'

14.substring(), Method returns a subset of a string between the start index and the end index , Or a subset from the beginning of the index to the end of the string . Parameter cannot be negative

let a = "ABC abc abc 123";
let b = a.substring(4, 7);
console.log(a); // 'ABC abc abc 123'
console.log(b); // 'abc'

15.match(), Returns an array of matching results , If there is no match , Then return to null. If the parameter is passed in a non regular expression object , Will use new RegExp(obj) Implicitly convert it to a regular expression object

let a = "ABC abc abc 123";
let b = a.match(/abc/g);
console.log(a); // 'ABC abc abc 123'
console.log(b); // ["abc", "abc"]

16.search(), lookup str Whether it matches a regular expression . If the match is successful , Returns the index of the first match of the regular expression in the string ; otherwise , return -1. If the parameter is passed in a non regular expression object , Will use new RegExp(obj) Implicitly convert it to a regular expression object

let a = "ABC abc abc 123";
let b = a.search(/abc/g);
console.log(a); // 'ABC abc abc 123'
console.log(b); // 4

版权声明
本文为[liangzn]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/08/20210804211905855g.html

Scroll to Top