当前位置:网站首页>JS string method

JS string method

2022-06-24 18:35:00 Brother Mengfan

Catalog

1、indexof()

2、charAt()

3、concat()

4、substring() 

5、substr()

6、slice() 

7、replace() 

8、replaceAll() 

9、split()

10、trim()

11、match()


1、indexof()

indexOf() Method returns the... That called it String The index of the specified value for the first time in an object , from fromIndex Search at
Cable . If the value is not found , Then return to -1.
The grammar is as follows :str.indexOf(searchValue [, fromIndex])
Example :
       var str1 = 'hello world';
        var p = str1.indexOf('l');
        console.log(p); //2
        p = str1.indexOf('l', 3);
        console.log(p); //3
        p = str1.indexOf('l', 4);
        console.log(p); //9
        p = str1.indexOf('llo');
        console.log(p); //2
        p = str1.indexOf('lloo');
        console.log(p); //-1

Be careful : stay indexof() The parameters of the , It can be a single character , Find it and return to its first position , Go back if you can't find it -1.

2、charAt()

charAt() Method returns the specified character from a string .
The grammar is as follows :str.charAt(index)
        var str = 'hello world';
        var c = str.charAt(7);
        console.log(c); //o
        c = str.charAt(-1);
        console.log(c); //  No output   Don't complain 
        c = str.charAt(20);
        console.log(c); //  No output   Don't complain 

Be careful : When the specified index value is not within the range of the string , No mistakes , But there will be no output .

3、concat()

concat() Method to join and merge one or more strings with the original string , Form a new string and return .
The grammar is as follows : str.concat(str2, [, ...strN])
Example :
        var str = 'hello';
        var s = str.concat(' world', '!');
        console.log(s); //hello world!

Be careful :

(1) Use concat() To splice strings is the same as using the plus sign to splice strings , But use concat() A little more elegant .

(2) But it should be avoided in development , Used especially in loops ( Memory footprint ).

4、substring() 

substring() Method returns a subset of a string between the start index and the end index , Or from the beginning of the index until the character
A subset of the end of the string .
The grammar is as follows :str.substring(indexStart[, indexEnd])
Example :
        var str = 'charset';
        console.log(str);
        var s = str.substring(1); // harset  Intercept from subscript to 1 To the end of the string  
        console.log(s);
        s = str.substring(1, 5);
        console.log(s); //hars  Left closed right away 
        s = str.substring(1, 3);
        console.log(s); // ha
        s = str.substring(3, 1);
        console.log(s); // ha   be equal to s = str.substring(1, 3);
        s = str.substring(-1);
        console.log(s); // If the first argument given is a negative number , Then it will copy the string and return .

explain :

(1) If only the first parameter is specified , Then the substring will run from the specified position to the end ;

(2). If both parameters are specified , Then the substring will change from the start position to the character before the end position ;( Left closed right away )

(3) If the second parameter is less than the first parameter , Then it will intercept from the back to the front ; ( It is equivalent to the result of parameter exchange )
(4) If the first argument given is a negative number , Then it will copy the string and return .

5、substr()

substr() Method returns a string intercepted at the start index position lenght individual .
The grammar is as follows :str.substr(from , length);
Parameter description :
(1)from, value type , It refers to the starting position of interception
(2) length, value type , Refers to the intercepted length
Example :
        var str = 'charset';
        console.log(str);
        var s = str.substr(1, 2);
        console.log(s); //ha
        s = str.substr(4, 2);
        console.log(s); //se     From the index number to 4 Intercept 2 position 
        s = str.substr(2);
        console.log(s); // arset  Cut to the end  
        s = str.substr(-2);
        console.log(s); // et    Return to intercept 

Be careful :

(1) The parameter of this method is from that position , How many characters to intercept .

(2) If both parameters are specified , Then the character of the specified length is intercepted from the specified position to form a new word

Fu string .
(3) If only the first parameter is specified , Then it will intercept from the specified position to the end of the string to form a new character
strand .
(4) If only one parameter is specified , And this parameter is negative , Will be inverted to form a new string .

6、slice() 

slice() Method to extract part of a string , And return a new string , And it doesn't change the original string .
The grammar is as follows : str.slice(beginIndex[, endIndex])
Example :
        var str = 'The morning is upon us.';
        console.log(str);
        var s = str.slice(5);
        console.log(s); //orning is upon us.
        s = str.slice(5, 9);
        console.log(s); //orni
        s = str.slice(5, -3);
        console.log(s); //orning is upon
        s = str.slice(-1, 5);
        console.log(s); // No output content 

explain :

(1) The function of this method and substr() Similar function of , Are used to intercept substrings .

(2) If only the first parameter is specified , From the specified position to the end of the original string .

(3) If you specify two parameters , Characters from the specified position to the end position will be extracted .

(4) If the second parameter is negative , The character from the specified position to the end of the number from the back to the front will be extracted .

(5) If the first argument is negative , There is no output , No mistake. .

7、replace() 

replace() Method returns a replacement value (replacement) Replace some or all of the patterns (pattern) After match
New string of . A pattern can be a string or a regular expression , The replacement value can be a string or a string
Callback function to be called for every match . If pattern Is string , Replace only the first match . The original string will not be changed
change .
The grammar is as follows :str.replace(regexp|substr, newSubStr|function)
Example :
        var str = 'Twas the night before Xmas...';
        console.log(str);
        //  Put the letters  e  Replace with  , 
        var s = str.replace('e', ',');
        console.log(s); //Twas th, night before Xmas...   Only one was replaced 
        s = str.replace(/\s+/ig, '-');
        console.log(s); //Twas-the-night-before-Xmas...    Replaced all spaces 

Be careful :

(1)replace() Method has two parameters , The first parameter represents the character to look for , The second parameter represents the
character ;
(2)replace() Method can support regular expressions , stay JS The regular expression in is written in // Between , Such as
/\s+/ Means to match one or more spaces . After the regular expression , You can also add parameters , Such as :i It means not distinguishing big
A lowercase letter ;g Represents a global match ;m Represents a match of multiple lines ;
(3) If it's a regular expression , You can't put this expression in quotation marks , If it is written in quotation marks, it will be regarded as a reference
Find a string to use .

8、replaceAll() 

replaceAll() Method returns a replacement value (replacement) Replace some or all of the patterns (pattern) Matches
New string after . A pattern can be a string or a regular expression , The replacement value can be a string or a
Callback function to be called for each match . If pattern Is string , Replace only the first match . The original string will not
change .
The grammar is as follows :str.replaceAll(regexp|substr, newSubStr|function)
Example :
        var str = 'TwAs the night before Xmas...';
        console.log(str);
        var s = str.replaceAll('as', ',');
        console.log(s); //TwAs the night before Xm,...
        s = str.replaceAll(/\s+/g, '-');
        console.log(s); //TwAs-the-night-before-Xmas...
        s = str.replaceAll(/as/ig, ',');
        console.log(s); //Tw, the night before Xm,...

Be careful :

(1)replaceAll() Method will replace all found characters ;

(2)replaceAll() Method if you want to use regular expressions , Then you must add a global match parameter (/g).

9、split()

split() Method uses the specified separator string to set a String Object is divided into substring arrays , With a specified split
String to determine the position of each split .
The grammar is as follows :str.split([separator[, limit]])
Example :
       var str = "Hello World How are you doing";
        console.log(str);
        var arr = str.split(' ');
        console.log(arr);
        arr = str.split(/\s+/);
        console.log(arr);

        
        str = "Hello,World,How,are,you,doing";
        console.log(str);
        arr = str.split(',');
        console.log(arr);

explain :

(1)split() Method cuts the string according to the specified delimiter , Generate an array ;

(2)split() Method supports regular expressions to cut .

10、trim()

trim() Method to remove white space characters from both ends of a string . The white space characters in this context are all white space characters
And all line terminators .
The grammar is as follows :str.trim()
Example :
        var str = '    char set     ';
        console.log(str);
        var s = str.trim();
        console.log(s); //char set    There is no space before or after    The space in the middle cannot be removed 

Be careful : Only the front and back spaces can be deleted , The space in the middle cannot be removed .

11、match()

match() Method returns the result of a string matching the regular expression .

The grammar is as follows :str.match(regexp)
Example :
        var str = 'For more information, see Chapter 3.4.5.1';
        console.log(str); //  Define a regular expression 
        var regex = /see (chapter \d+(\.\d)*)/i;
        var flag = str.match(regex);
        console.log(flag);

原网站

版权声明
本文为[Brother Mengfan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211333167125.html