The common method of string-learn you will be invincible!

Posted May 27, 20204 min read

Engaged in front-end work, I usually collect some notes and put them in the notes. I found that the simplest basic knowledge is the most commonly used in work. Sometimes it is ambiguous. It can be seen that the basic knowledge is not solid. , Consolidate the basic knowledge, you will be invincible after learning!

Commonly used methods in strings:

str.toLowerCase();
toLowerCase() lowercase all strings

var str = "Hello World";
var str1 = str.toLowerCase();
console.log(str); //Hello World
console.log(str1); //hello world

str.toUpperCase()
toUpperCase() capitalize all strings

var str = "Hello World";
var str1 = str.toUpperCase();
console.log(str); //Hello World
console.log(str1); //HELLO WORLD

str.slice()
slice():returns the substring extracted from the string

var str = "Hello World";
var str1 = str.slice(); //If there is no parameter, it is the copy string
var str2 = str.slice(2); //If there is only one parameter, start from subscript 2 to the end of the line, copy
var str3 = str.slice(2,7); //From subscript 2 to the end of 7, without subscript 7, return the copied string
var str4 = str.slice(-5, -1);
Equivalent to
var str4 = str.slice(11 +(-5), 11 +(-1));
var str4 = str.slice(6,10);

Pay attention here
A number must be less than the latter, otherwise an empty string is returned

var str5 = str.slice(8,1);
//If there is only one parameter, it is the copy string
console.log(str1); //Hello World
console.log(str2); //llo World
console.log(str3); //llo W
console.log(str4); //Worl
console.log(str5); //""

str.substring()
substring():Extract characters between two specified subscripts in the string.
Same as slice(), except that substring() does not accept negative numbers

var str = "Hello World";
var str1 = str.substring();
var str2 = str.substring(-2, -7);
console.log(str1); //Hello World
console.log(str2); //""

str.substr()
str.substr() returns a substring of a specified length starting from a specified subscript

var str = "Hello World";
var str1 = str.substr(); //Copy string
var str2 = str.substr(1); //Copy string from subscript 1 to the end of the line
var str3 = str.substr(1,3); //From subscript 1, count 3 elements to copy the string
var str4 = str.substr(-5,3); //Start with subscript 11 +(-5) = 6, count 3 elements Copy the string
console.log(str1); //Hello World
console.log(str2); //ello World
console.log(str3); //ell
console.log(str4); //Wor

str.split()
split():Split the string into a string array.

var str = "1:2:3:4:5:6:7";
var str1 = str.split(""); //Each character is separated and is an element
var str2 = str.split(":"); //To:separate into an element
var str3 = str.split(":", 4); //The maximum length can be separated by 4 lengths, which can be regarded as intercepting the first 4 digits

console.log(str1); //["1", ":", "2", ":", "3", ":", "4", ":", "5", ":", "6", ":", "7"]
console.log(str2); //["1", "2", "3", "4", "5", "6", "7"]
console.log(str3); //["1", "2", "3", "4"]

str.indexOf()
indexOf():Returns the first occurrence of a specified substring in the string

var str = "HelloswjnhWorld";
var str1 = str.indexOf("W"); //Case sensitive, the default is to start from the subscript 0
var str2 = str.indexOf("a"); //If not found, return -1

console.log(str1); //10
console.log(str2); //-1

str.lastIndexOf()
lastIndexOf():Returns the last position of a specified substring in the string.

var str = "HelloswjnhWorldW";
var str1 = str.lastIndexOf("W"); //case sensitive, the default is to start from the subscript 0, the last position
var str2 = str.lastIndexOf("a"); //If not found, return -1

console.log(str1); //15
console.log(str2); //-1

str.charAt()
charAt():Returns the character at the specified subscript position. If index is not between 0-str.length(excluding str.length), return an empty string.

var str = "HelloswjnhWorldW";
var str1 = str.charAt(2); //Return the character at the specified subscript position
var str2 = str.charAt(18); //If not found, return ""

console.log(str1); //l
console.log(str2); //""

str.charCodeAt()
charCodeAt():returns the unicode encoding of the character at the specified subscript position, this return value is an integer between 0-65535.

var str = "HelloswjnhWorldW";
var str1 = str.charCodeAt(2); //Return the character at the specified subscript position
var str2 = str.charCodeAt(18); //If not found, return NAN

console.log(str1); //108
console.log(str2); //NAN

str.replace()
replace():Replace some characters with some characters in the string, or replace a substring that matches the regular expression.

var str = "HelloswjnhWorldW";
var reg =/o/ig; //o is the keyword to be replaced, and cannot be quoted, otherwise the replacement will not take effect, i ignores the case, and g means global search.
var str1 = str.replace(reg, "**");

console.log(str1); //Hell **swjnhW** rldW

str.match()
match():returns an array of all searched keyword content.

var str = "HelToswjnhtorldW";
var reg =/to/ig;
var str1 = str.match(reg);

var reg2 =/heo/ig;
var str2 = str.match(reg2);

console.log(str1); //["To", "to"]
console.log(str2); //null returns null if not found

Editor is not easy, if there is any harvest, WeChat rewards editor to drink Wahaha
image.png

Single dog's sunflower collection, a must-have for sisters, stay tuned!
image.png