1.trim()
The trim() method removes the white space characters at both ends of the string. The white space characters in this context are all white space characters
let getting = ' Hello Word' console.log(getting); //' Hello Word' console.log(getting.trim()); //'Hello Word'
2. length
Length indicates the length of a string
let x = 'hello'; let a = ''; console.log('this x The length of the string is' + x.length); //The length of this x string is 5 console.log('a The length of the is' + a.length); //The length of a is 0
3. split()
The split() method uses the specified split String to split a String object into an array of multiple strings, and uses a specified split String to determine the position of each split
const str='the quick brown fox jumps over the lazy dog' const world=str.split(' ') // ['the', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog'] //console.log(world); const chars=str.split('') //['t', 'h', 'e', ' ', 'q', 'u', 'i', 'c', 'k', ' ', 'b', 'r', 'o', 'w', 'n', ' ', 'f', 'o', 'x', ' ', 'j', 'u', 'm', 'p', 's', ' ', 'o', 'v', 'e', 'r', ' ', 't', 'h', 'e', ' ', 'l', 'a', 'z', 'y', ' ', 'd', 'o', 'g'] //console.log(chars); console.log(chars[4]); //q const strCopy=str.split() //['the quick brown fox jumps over the lazy dog'] console.log(strCopy);
4. join()
The join() method can realize string splicing
const str=['Hello','Can we be friends'+'!'] let join=str.join() console.log(join); //Hello, can we be friends! const str = ['the', 'quick', 'fox' ,'jumps', 'over' ,'the', 'lazy' ,'dog'] console.log(str.join('-')); //the-quick-fox-jumps-over-the-lazy-dog
5. replace()
The replace() method returns a new string after replacing some or all of the pattern matches with the replacement value. The 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 the match is made. The original string will not change
const str='the quick fox jumps over the lazy dog' console.log(str.replace('dog','pig')); //the quick fox jumps over the lazy pig // Regular matching const regx=/dog/i; console.log(str.replace(regx,'monkey')); //the quick fox jumps over the lazy monkey
6. indexOf()
The indexOf() method returns the specified index that appears for the first time in the String object calling it. If the value cannot be found, it returns - 1
const str = 'the quick fox jumps over the lazy dog'; const search='dog'; const indexOfFIRST=str.indexOf(search) console.log(indexOfFIRST); console.log(`the index of the first ${search} from the beginng is${indexOfFIRST}`); //Indexof (search, 4) the value of the string to be searched by serch. 4 means to start searching from the first bit. If it is not written, it is 0 by default console.log(str.indexOf(search,4));
Use indexOf to count the number of times a letter appears in a string
let str = 'to be,or not to be, that is the question ' let count = 0; let pos = str.indexOf('t') for (let i = 0; i < str.length; i++) { if (pos != -1) { count++; pos = str.indexOf('t', pos + 1) console.log(pos); } } console.log(count);
7. toLocaleLowerCase(),toLocaleUpperCase()
Convert string to case
let str='ABC'; // Convert to lowercase console.log(str.toLocaleLowerCase()); let str1='hello' // Convert to uppercase console.log(str1.toLocaleUpperCase());
8. repeat()
repeat() constructs and returns a new string that contains a specified number of copies of strings that are concatenated together. (duplicate construction string)
let str='ABC'; //3 represents the number of times to repeat console.log(str.repeat(3));//ABCABCABC
9. substr()
The substr() method returns the characters in a string from the specified position to the specified number of characters
let str='theoftheujikkj' // 3 represents the position where the interception starts, starting from 0 by default, and 5 represents the length of the string to be extracted console.log(str.substr(3,5));//ofthe
10. slice()
slice() method extracts a part of a string and returns a new string without changing the original string. Two parameters can be passed. The first parameter is the index value to be extracted, which is 0 * * by default. If the value is negative, it will be treated as strLength + beginIndex * *. Here strLength is the length of the string (for example, if beginIndex is - 3, it will be regarded as strLength - 3). The second value is the index value to end. If the second parameter is not written, it will be extracted until the end by default. If the parameter is negative, It is regarded as strLength + endIndex, where strLength is the length of the string (for example, if endIndex is - 3, then strLength - 3).
let str='theoftheujikkj' // Write only the first parameter, and intercept to the last by default console.log(str.slice(5)); //theujikkj // The number intercepted by the second parameter is the first digit of the index value console.log(str.slice(5,8));//the console.log(str.length); //14 console.log(str.slice(-3)); //kkj console.log(str.slice(-3,-1)); //kk