1. Find the string.
indexOf
(Find from front to back.)lastindexOf
(Look from back to front.)
indexOf
('Need to find the character', start looking for from this index)
Returns the index position found, or -1 if not found.
var str = " abc defa";
console.log(str.indexOf('c',2));//1
search
method.
console.log(str.search("f"));//7
2. Element replacement.
replace
method.
console.log(str.replace("a", 's'));//sbc defa
When replacing all identical characters in a string, regular expressions are required.
console.log(str.replace(/a/ig, 's'));//sbc defs
Has no effect on the original string.
3. The string is empty.
trim
method.
console.log(str.trim());//abc defa
If you want to remove all the spaces in the middle, you need to use regularization.
console.log(str.replace(/\s/g, ''));//abcdefa
4. String splicing.
concat
with +
.
var s1 = 'hdbhjbj';
var s2 = 'fdsadsadsa';
console.log(str.concat(s1, s2));//hdbhjbjfdsadsadsa
5. Get the character according to the character index.
charAt
method.
console.log(str.charAt(1));//a
6. Return the ASCII value of the character according to the index.
charCodeAt
method.
console.log(str.charCodeAt(1));//97
7. String interception.
substr
(start position, length)substring
(Start position, end position) take the smaller and not the larger.
console.log(str.substr(0,3));
console.log(str.substring(0, 6))
Has no effect on the original string.
8. Return the array after splitting by symbol.
split
method.
var s3 = "abcdefg";
console.log(s3.split(''));//['a','b','c','d']
9. Use the b tag to make it bold.
bold
.big
.blink
.
10. Convert case.
Capitalize: toUpperCase
, toLacalUpperCase
.
Lower case: toLowerCase
, toLocalLowerCase
.
console.log(str.toLowerCase()); //abc defa
console.log(str.toUpperCase());//ABC DEFA
console.log(str.toLocaleLowerCase());// abc defa
console.log(str.toLocaleUpperCase());//ABC DEFA
11. Numbers are accurate to the decimal point.
toFixed
method.
var num = 99;
console.log(num.toFixed(2));//99.00
12. Compare character ASCII values.
localeCompare
method.
var f1 = 'a';
var f2 = 'c';
console.log(f2.localeCompare(f1));//1
The big one is 1, and the small one is -1.