I. Declaration string:
VaR normal_monkey = "I am a monkey! <Br> ";
Document. writeln ("normal monkey" + normal_monkey );
VaR bold_monkey = normal_monkey.bold ();
Document. writeln ("bold monkey" + bold_monkey );
The declaration here: var bold_monkey = normal_monkey.bold ();
It is equivalent to the following statement:
VaR bold_monkey = "<B>" + normal_monkey + "</B> ";
The declaration of version 1st looks much more concise. The string object is used here.
Other string objects include indexof, charat,
Substring, and split. These methods can go deep into the string structure.
First, let's look at indexof.
2. indexof
Indexof is used to locate a series of characters in a string and tell you the starting position of the substring. For example
If the middle of a string contains this substring, indexof returns "-1 ."
Example:
VaR the_word = "monkey ";
// Let's start with the word "monkey.
VaR location_of_m = the_word.indexof ("M ");
// Location_of_m (where the letter M is located) will be 0, because the letter M is located at the starting position of the string.
VaR location_of_o = the_word.indexof ("O ");
// Location_of_o (where the letter O is located) will be 1.
VaR location_of_key = the_word.indexof ("key ");
// Location_of_key (Key location) will be 3 because the sub-string "key" starts with the letter K, and K
The position in the word monkey is 3.
VaR location_of_y = the_word.indexof ("Y ");
// Location_of_y) the position of Y is 5.
VaR cheeky = the_word.indexof ("Q ");
// The cheeky value is-1, because there is no letter q in the word "monkey.
Indexof is more useful:
VaR the_email = prompt ("what's your email address? ","");
VaR the_at_is_at = the_email.indexof ("@");
If (the_at_is_at =-1)
{
Alert ("You loser, email addresses must
Have @ signs in them .");
}
This sectionCodeAsk the user's email address. If the email address entered by the user does not contain characters
"@ The email address you entered is invalid. The email address must contain the character @. "
3. charat
The chatat method is used to discover characters at a specific position in a string.
Here is an example:
VaR the_word = "monkey ";
VaR the_first_letter = the_word.charat (0 );
VaR the_second_letter = the_word.charat (1 );
VaR the_last_letter = the_word.charat (the_word.length-1 );
The_first_letter (1st characters) is "M"
The_second_letter (2nd characters) is "O"
The_last_letter (the last character) is "Y"
Note that you can use the Length attribute of a string to find the number of characters in length. In this example,
The_word is "monkey", so the_word.length is 6. Do not forget to include 1st characters in a string
The position is 0, so the last character is length-1. So we used
The_word.length-1.>
4. substring)
The sub-string (substring) and charat have some similarities. The difference is that it can capture the entire
A substring, not just a letter. The format is as follows:
VaR the_substring = the_string.substring (from, );
"From" refers to the position of 1st letters in a substring. "to" is a bit odd. It is later than the last character in the substring.
You can use this magic method to mark the start and end positions of a substring.
The length of the substring is obtained after the position of "to" minus "from:
VaR the_string = "monkey ";
VaR clergy = the_string.substring (0, 4 );
VaR tool = the_string.substring (3, 6 );
After the code is run, the clergy value of the variable is "Monk", and the tool value of the variable is "key ".
The sub-string is often used with indexof to divide the string into several blocks. For example,
You can extract the domain name from a given URL:
VaR the_url = prompt ("what's the URL? ","");
VaR lead_slashes = the_url.indexof ("//");
VaR domain_start = lead_slashes + 2;
VaR without_resource = the_url.substring (domain_start, the_url.length );
VaR next_slash = without_resource.indexof ("/");
VaR domain = without_resource.substring (0, next_slash );
This Code indicates that if you enter
"Http://www.webmonkey.com/javascript/index.html";, the domain name is
"Http://www.webmonkey.com/". If this method is a little tricky for you, I will show you how to use split
Method to simplify the execution process, but first we do some analysis.
The basic technique is to separate the content between 1st and 2nd slashes:
VaR the_url = prompt ("what's the URL? ","");
// This line of code asks the user for a URL. Assume that the user has entered
"Http://www.webmonkey.com/javascript/index.html .";
VaR lead_slashes = the_url.indexof ("//");
This line of code determines the position of the first double slash. In this example, the lead_slashes value is 5, because the double slash bit
Set from 5.
You may think that the URLs generally start with http: //, so the double slash must start at 5.
Do you need to add the extra code indexof? But the key to the problem is that you do not know that the user is filling in the URL
Are you sure you want to fill in http:, they may accidentally enter a space, maybe they typed the URL in
On an encrypted server, its URL is "https://www.whatever.com/";. In programming you must anticipate various
The problem may occur. Therefore, we must use the indexof method to determine the exact start position of the double slash.
VaR domain_start = lead_slashes + 2;
This line of code is used to calculate the starting position of the domain name's 1st letters. Because there is a double slash, the domain name
The starting position of the 1st letter should be added to the position of the double slash plus 2.
VaR without_resource = the_url.substring (domain_start, the_string.length );
This code extracts all the characters after the starting position of the domain name.
Without_resource is "www.webmonkey.com/javascript/index.html ."
VaR next_slash = without_resource.indexof ("/");
This line of code calculates the position of the next Slash in the string, from the starting position of the string to the slash
In this example, the position of the next Slash is 17.
VaR domain = without_resource.substring (0, next_slash );
The last step is to extract all content from the starting position of the string to the next Slash. In this example
Equivalent to "http://www.webmonkey.com /".
This is really troublesome. Using the split method can make the process much easier.>
5. Splitting Method)
You can use the split method to separate a series of names with a limiter, and then
Put it in an array. For example:
VaR my_friends = "Trixie, moxie, Sven, Guido, Hermes ";
VaR friend_array = my_friends.split (",");
For (loop = 0; loop <friend_array.length; loop ++)
{Document. writeln (friend_array [loop] + "is myfriend. <br> ");}
This code splits the string my_friends into an array containing five elements. javascript can be automatically created for you.
Create an array, so you do not need to use new array ().
After the string is split into an array, we use a loop statement to write each name. We can use the split side
To simplify the domain name extraction:
VaR the_url = prompt ("what's the URL? ","");
VaR first_split = the_url.split ("//");
VaR without_resource = first_split [1];
VaR second_split = without_resource.split ("/");
VaR domain = second_split [0];
This code is much simpler and easier to understand. Let's analyze this Code:
VaR the_url = prompt ("what's the URL? ","");
Prompt the user to enter a URL.
"Http://www.webmonkey.com/javascript/index.html ";.
VaR first_split = the_url.split ("//");
Split the string you entered into two parts: first_split [0] is "http:", first_split [1] is
"Www.webmonkey.com/javascript/index.html ."
VaR without_resource = first_split [1];
// Extract the 2nd elements in the array, so now without_resource is
"Www.webmonkey.com/javascript/index.html ."
VaR second_split = without_resource.split ("/");
Divide without_resource into three parts: http://www.webmonkey.com, JavaScript/, and index.html.
Can you see the purpose of split?
VaR domain = second_split [0];
Now, we can extract the 1st elements in the new array to get the domain name.