I. CharAt (index) returns the character of one index of a string.
Syntax: Str.charat (index);
var str= ' I am Chinese '; Console.log (Str.charat (3)); // output "Country", index number starting from 0, passing in the number 3, is to get the character indexed to 3
Two. IndexOf ("str") gets the index number of a character on the string for the first time and cannot find the output of the current character-1;
Syntax: Str.indexof ("str");
var str= ' I am Chinese '; Console.log (Str.indexof ("fruit")); // output-1, if the current character is not found, the output is-1; Console.log (Str.indexof ("state")); // output 3, get to "country" index number is 3
Three. Slice (Beginindex,endindex); Extracts a part of the string and returns the new string;
Syntax: Str.slice (BEGININDEX,ENDINDEX);
Parameter beginindex: Required, indicating the index value at which to start the fetch location
Parameter endindex: Optional, indicates the first character from index number A to index B, and if not, gets all the elements behind the current index number;
var str= ' I am Chinese '; Console.log (Str.slice (2)); // output "Chinese", followed by no parameters to get all the characters after the index; Console.log (Str.slice (2, 4)); // output "China", which represents the first character from index number A to the index number B; Console.log (Str.slice (2,-2)); // The output "medium", means to get the 2nd character to the 2nd character, that is, "Chinese character"; Console.log (Str.slice (-2)); // output "People", if the first parameter is passed in as a negative number, if it is-2, then the last two characters of the countdown is obtained;
Four. substr (Index,length); Returns a substring from the specified position in a string to a specified length;
Syntax: Str.substr (index,length);
Parameter: Index, required, indicates the indexed value at which to begin extracting the position;
Parameter: length, optional, second parameter indicates the length of the get character, length
or substr
an empty string if it is 0 or negative
var str= ' I am Chinese '; Console.log (Str.substr (2)); // output "Chinese", if the second argument is not passed, get all characters starting from the index number to the back Console.log (Str.substr (-2)); // output "People", if index is negative, then start from the countdown to get Console.log (Str.substr (2,2)); // output "China", the second parameter indicates the length of the get character, and if length is 0 or negative, SUBSTR returns an empty string
Several ways to manipulate strings commonly used in JavaScript charat (), indexOf (), Slice (), substr ()