JS string method

catalogue

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 String The index of the specified value that first appears in the object, from fromIndex Search at
Cable. Returns if the value is not found -1 .
The syntax 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

Note: in the parameter of indexof(), it can be a single character. If it is found, it will return to its first position, and if it is not found, it will return - 1.

2,charAt()

charAt() Method returns the specified character from a string.
The syntax 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, no error
        c = str.charAt(20);
        console.log(c); // No output, no error

Note: when the specified index value is not within the range of the string, there will be no error, but there will be no output.

3,concat()

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

be careful:

(1) Using concat() to splice strings is the same as using a plus sign to splice strings, but concat() is a little more elegant.

(2) However, it should be avoided in development, especially in loops (memory occupation).

4,substring() 

substring() Method returns a subset of a string from the start index to the end index , Or from start index to character
A subset of the end of the string.
The syntax is as follows: str . substring ( indexStart [, indexEnd ])
Example:
        var str = 'charset';
        console.log(str);
        var s = str.substring(1); // harset intercepts the character with subscript 1 to the end of the string 
        console.log(s);
        s = str.substring(1, 5);
        console.log(s); //hars left closed right open
        s = str.substring(1, 3);
        console.log(s); // ha
        s = str.substring(3, 1);
        console.log(s); // ha equals s = str.substring(1, 3);
        s = str.substring(-1);
        console.log(s); //If the first parameter given is a negative number, it will copy the string and return it.

explain:

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

(2). If both parameters are specified, the substring will be the character from the start position to the end position; (left closed right open)

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

5,substr()

substr() method returns a string and intercepts lenght at the starting index position.
The syntax is as follows: str . substr ( from , length );
Parameter Description:
(1) from , numeric type, refers to the starting position of interception
(2) length , numeric 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 truncates 2 digits from the index number of 4
        s = str.substr(2);
        console.log(s); // arset intercepts to the end 
        s = str.substr(-2);
        console.log(s); // et return intercept

be careful:

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

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

Character string.
(3) If only the first parameter is specified, it will intercept from the specified position to the end of the string to form a new character
String.
(4) If only one parameter is specified and the parameter is negative, it will be intercepted reversely to form a new string.

6,slice() 

slice() Method extracts a part of a string and returns a new string without changing the original string.
The syntax 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 is similar to that of substr(), which is used to intercept substrings.

(2) If only the first parameter is specified, it will go from the specified position to the end of the original string.

(3) If two parameters are specified, characters from the specified position to the end position are extracted.

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

(5) If the first parameter is negative, there is no output and no error will be reported.

7,replace() 

replace() Method returns a value replaced by( replacement )Replace some or all of the modes( pattern )After match
New string for. The pattern can be a string or a regular expression, and the replacement value can be a string or a regular expression
Callback function to be called for each match. If pattern Is a string, only the first match is replaced. The original string will not be changed
Change.
The syntax is as follows: str . replace ( regexp | substr , newSubStr | function )
Example:
        var str = 'Twas the night before Xmas...';
        console.log(str);
        // Replace the letter e 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 find and the second parameter represents the character to replace
Character;
(2)replace() Method can support regular expressions JS Need to be written in regular expressions // Between, e.g
/\s+/ Indicates matching one or more spaces. After the regular expression, you can also add parameters, such as: i Indicates that it does not distinguish between large and small
a lowercase letter; g Indicates global matching; m Indicates matching multiple lines;
(3) If it is a regular expression, it cannot be written in quotation marks. If it is written in quotation marks, it will be regarded as a query
Find a string to use.

8,replaceAll() 

replaceAll() Method returns a value replaced by( replacement )Replace some or all of the modes( pattern )Matches
New string after. The pattern can be a string or a regular expression, and the replacement value can be a string or a regular expression
Callback function to be called for each match. If pattern Is a string, only the first match is replaced. The original string will not
Change.
The syntax 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) The replaceAll() method will replace all the characters found;

(2) If you want to use regular expressions in the replaceAll() method, you must add the global matching parameter (/ g).

9,split()

split() Method uses the specified delimiter string to String Object is split into an array of substrings to a specified split
String to determine the location of each split.
The syntax 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) The split() method will cut the string according to the specified separator to generate an array;

(2) The split() method supports regular expressions for cutting.

10,trim()

trim() Method removes white space characters from both ends of a string. White space characters in this context are all white space characters
And all line terminator characters.
The syntax is as follows: str . trim ()
Example:
        var str = '    char set     ';
        console.log(str);
        var s = str.trim();
        console.log(s); //char set has no spaces before and after, and the spaces in the middle cannot be removed

Note: only the spaces before and after can be deleted, and the spaces in the middle cannot be removed.

11,match()

The match() method retrieves and returns the result of a string matching the regular expression.

The syntax 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);

Keywords: Javascript Front-end

Added by waygood on Mon, 14 Feb 2022 14:54:04 +0200