The most string manipulation encountered in the write front-end process is to determine if a string contains a character, and to intercept a string.
String operation there are many ways, in fact, as long as the master above two is enough, other methods with him, good, the following two ways to explain the above.
1, judge whether a string contains a certain character, using the indexof () method;
Str.indexof ("Parameter 1", "Parameter 2"), parameter 1 indicates whether a small string is included, the parameter 2 indicates the starting position from left to right, and the default starts at 0, and Str represents the original string to look for, PS: If the original string contains more than one judgment string, only the first position is returned.
For example, to determine whether the image name "Title_clicked.png" contains the string "clicked", the code is as follows
var imageName = "Title_click.png"; alert (Imagename.indexof ("clicked"));
This will pop up 6, indicating that the click string is found in the 6th place in the subscript order.
Returns "1" if not included;
2, the interception of a string, the use of substring () method;
SUBSTRING ("Parameter 1", "Parameter 2"), parameter 1 is the starting position of the Intercept, the parameter 2 means the end position of the Intercept, PS: The interception principle is "Gu Tou disregard tail", meaning that the truncated string contains the starting position character, does not contain the end position character. such as "123". substring (0,2); The returned result is "12";
For example: Operation "Title_clicked.png" Remove the name "_clicked", the method is as follows:
var imageName = "Title_clicked.png"; var index1 = Imagename.indexof ("_clicked"); var index2 = Imagename.indexof (".") ); var newName = Imagename.substr (0,index1) + imagename.substr (INDEX2);
At this time the return of the NewName is "title.png";
Finish!
Determine if a string contains a character? How to intercept a certain character?