String manipulation methods
increase
1.concat
Note 1: the original string is not changed and a new string is returned
var str='abc'
var newarr = str.concat('b')
console.log(newarr);// abcb
Copy the code
Of course you can concatenate strings with the usual ${} or +
delete
1.slice
Note 2: Parameter 1 represents the index of start and parameter 2 represents the index of end
var str='abc'
var newarr = str.slice(0.1)
console.log(newarr);//a
console.log(str);//abc
Copy the code
2.substr
Note 2: Substr argument 1 is the index of start, and argument 2 is truncated
var str='abc nba'
var newarr = str.substr(2.4)
console.log(newarr); //c nb
console.log(str);//abc nba
Copy the code
3.substring
Note 2: The index of substring 1 is the index of start, and the index of substring 2 is the index of end
var str='abc nba'
var newarr = str.substring(2.4)
console.log(newarr);
console.log(str);
Copy the code
So what’s the difference with Slice? Look at the following example
var str='how,are,you'
console.log(str.slice(3, -4));//-4: The value is' from -1 forward. The value of '3 'is'. The value of' is'
console.log(str.substring(3, -4)); // select * from (1); // select * from (1)
Copy the code
To change the operation of the
1.trim
Note 1: Remove whitespace before and after the string. Note 2: Return the string with whitespace removed without changing the original string
var str=' how,are,you '
console.log(str.length);/ / 13
console.log(str.trim());//how,are,you
console.log(str.trim().length);/ / 11
console.log(str);// How,are,you (there are Spaces before and after)
Copy the code
2.trimLeft/trimRight
Note: Remove left space/remove right space
var str=' how,are,you '
console.log(str.trimLeft());
console.log(str.trimLeft().length);/ / 12
var str=' how,are,you '
console.log(str.trimRight());
console.log(str.trimRight().length);/ / 12
Copy the code
3.repeat
Note: Accept an integer indicating how many times the string is copied
var str=' how,are,you '
console.log(str.repeat(2));// how,are,you how,are,you
Copy the code
4.toLowerCase/toUpperCase
Note: toLowerCase converts the string toLowerCase, and toUpperCase converts the string toUpperCase
var str=' how,are,You '
console.log(str.toLowerCase());// how,are,you
console.log(str.toUpperCase());// HOW,ARE,YOU
Copy the code
Check the operation of the
1.charAt
Note: charAt() passes in a number and looks up the corresponding element based on index
var str='how,are,You'
console.log(str.charAt(2));//w
Copy the code
2.indexOf
Note: indexOf() passes in the element you are looking for and returns index; if not found, returns -1
var str='how,are,You'
console.log(str.indexOf('h'));/ / 0
Copy the code
3.includes
Note that: includes() returns true/false when passed to the element you are looking for
var str='how,are,You'
console.log(str.includes('h'));
Copy the code
4.startWith
Note: The startWith() string searches for whether it starts with a string, returning true/false
var str='how,are,You'
console.log(str.startsWith('ho'));//true
console.log(str.startsWith('o'));//false
Copy the code
Methods of transformation
1.split
Note: Convert strings to arrays
var str='how,are,You'
console.log(str.split(', '));//["how", "are", "You"]
Copy the code
String of common questions
1. Count the number of occurrences of a string
Var STR =’abcbaa’
var str='abcbaa'
var index = str.indexOf('a')
var num=0
while(index! = -1) {/ / found
num++
// Find the first one and start at the next position
index = str.indexOf('a',index+1)}console.log(num);
Copy the code
2. Return characters based on position
var str='aabb'
console.log(str.charAt(0));
Copy the code
3. Count the elements that appear most frequently
Ideas: String into an array, using the array reduce statistics
var str='abbanb'
var newstr = str.split(' ').reduce((pre,item,index) = >{
if(! pre[item]) { pre[item]=1
}else{
pre[item]++
}
return pre
},{})
var max=0
var str=' '
for (const key in newstr) {
if (newstr[key]>max) {
max=newstr[key]
str=key
}
}
console.log(newstr);
console.log(max,str);
Copy the code
4. Quantification of numbers
1. The use of ApitoLocaleString
console.log(
(1234567890).toLocaleString('en-US'));/ / 1234567890
Copy the code
2. Use the reduce
1234 4321 index=3 index= 1 1+’,’+pre
const num = String(1234567890);
var newNum = num.split(' ').reverse().reduce((pre,item,index) = >{
return index%3= = =0? item+', '+pre:item+pre
})
console.log(newNum);
Copy the code