Common javascript string functions
The most common string methods are: indexof (), charat (), and substring ()
Indexof () function:
This function allows you to determine whether a string exists in a longer string and its location. It is equivalent to the strstr function in C language and the instr function in Visual Basic language. This method also has a corresponding function: lastindexof (), which is searched at the other end of the function string.
As the function name means, the return value indicates the position of the string in the searched string. If no string is found in the searched string,-1 is returned. In JavaScript,-1 is a normal integer, not a Boolean number. The following is an example:
VaR mystring = "have a nice day! ";
Alert (mystring. indexof ("A"); // here 1 is returned
Alert (mystring. lastindexof ("A"); // here 13 is returned (where the last A is located)
The index of arrays in Javascript starts from 0, which is the last word in C language. Therefore, the preceding statement alert (mystring. indexof ("A") returns 1 because "A" is in the string "have a nice day! .
Be careful. You may find the string "have a nice day! "There is another character" A ". How can we find the second letter"? This is a good problem. To do this, we must introduce the second parameter of this function. The second parameter is an integer indicating the start position of the string.
The code below is used to find all the characters "A". The specific code is as follows:
VaR mystring = "have a nice day! ";
VaR Index = mystring. indexof ("");
While (index! =-1 ){
Alert (INDEX );
Index = mystring. indexof ("A", index + 1 );
// Start search after last match found
}
This code details: the variable index is initialized to the location where the first "a" is located (if there is no "A", the variable index is-1 ). Then a loop is given, with the condition "index! =-1. In each loop, we add the variable index to 1, that is, starting with the first character after "a", until all the characters "A" are found. When no character "a" is found in the string variable mysring,-1 is returned. At this time, the index is equal to-1 and does not meet the cyclic Condition Index! =-1, so the loop ends. The output of the alert (INDEX) statement is:, 13.
Charat () function:
This function returns the character in the string at the given position. Essentially, it is a special case of the substring () method, but it also has its own purpose. If you are a C-language programmer or a programmer in a similar language, you can understand that when referencing characters, you can use string. charat (INDEX) to replace string [Index].
Next we will use this function in a form input. The form contains an email address. Of course, this email address must contain characters, numbers, and a "@" symbol. We can force a single character in the string column. The Code is as follows:
<Script language = "JavaScript"> <! -- Hide from older browsers
VaR parsed = true;
VaR validchars = "abcdefghijklmnopqrstuvwxyz0123456789 @.-";
VaR email = prompt ("What is your email address? ", Nobody@nowhere.com );
For (VAR I = 0; I <email. length; I ++ ){
VaR letter = Email. charat (I). tolowercase ();
If (validchars. indexof (letter )! =-1)
Continue;
Alert ("invalid character:" + letter );
Parsed = false;
Break;
}
If (parsed) Alert ("your email address contains all valid characters .");
// Stop hiding -->
</SCRIPT>
Substring () function:
This function is usually used to extract any part of a string. Its parameters are 'start' and 'end '. The starting value is the index of the first character, and the ending value is the index of the first character after the part is returned. You may feel confused, but the best way to remember this is to return a string with the length equal to end-start.
If the second parameter is omitted, It defaults to the end of the string. Below are several examples: var STR = "this is a string ";
Str. substring (1, 3); // The result is hi.
Str. substring (3, 1); // The result is hi.
Str. substring (0, 4); // The result is this
Str. substring (8); // The result is hi.
Str. substring (8, 8); // The result is null.
The second example above shows that when Start> end, the two parameters are automatically converted. The final example shows that when start is equal to end, the return value is a null string.