Learning AJAX + JQUERY (Basic JavaScript objects)

Source: Internet
Author: User
Tags string indexof

What is an object?
To put it simply, the objects in programming languages are simplified for things in reality. For example, we are an object, but it is difficult for programming languages to fully describe such a complex object. Therefore, we must simplify the process. First, we should simplify people into a combination of attributes and actions, and then only keep the attributes and actions that are meaningful to the program. For example, if we develop a program to count the heights of people in a school, we can omit the human behaviors in this program and only keep the behaviors, only the height attribute is retained. In this way, we get the simplest object.
JavaScript string object
This section describes the attributes and methods of strings, which are mainly used to process text.
Object Attributes
In fact, we are already using objects in html dom. For example, we know that DOM nodes have some information, such as nodeName and nodeType, which are actually the attributes of node objects. The following uses a string as an example to check the attributes of an object.
String attributes
Var s = "I have 7 characters ";
After the s string is defined, we have a string object. We can access its length attribute. This attribute does not need to be defined. It is a built-in attribute. The access method is as follows:
S. length
Click the button below to check the length of the string.
Form top
Alert (s. length)
Bottom of the form
String object method (behavior)
Similarly, an object can have behavior. Taking a String object as an example, we can let the string return letters or texts at a certain position. This is an action. You can use the buttons below to experience the attributes and methods of the string. This article will explain in detail how to use each method.
<Script type = "text/javascript">
Var s = "I have 7 characters ";
Var str = "character" + "string"; // Add two strings
</Script>

 
Form top
Alert (str)
Use the length attribute of the string to set the length of the string.
Alert (str. length)
Use the charAt method to return the characters at the specified position.
Alert (str. charAt (0 ))
Alert (str. charAt (1 ))
The substring method truncates a substring from a string.
Alert (str. substring (0, 2 ))
IndexOf returns the position of the specified character or string in the string. If the character does not exist,-1 is returned.
Alert (str. indexOf ('delimiter ')
The lastIndexOf method returns the position of the last occurrence of a character in a string.
Bottom of the form
JavaScript string length
The length attribute of a string can return the length of a string, that is, the number of characters contained. As shown in the following example, we have created four strings. click the button below to display them. The Code is as follows:
JavaScript code
<Script type = "text/javascript">
Var str0 = "";
Var str1 = "short ";
Var str2 = "two ";
Var str3 = "this is a long string ";
</Script>
Test the length attribute of a string.
Since the first string is null, The length attribute is 0.
Alert (str0.length)
Alert (str1.length)
Alert (str2.length)
Alert (str3.length)
JavaScript string charAt Method
The charAt method of a string can return characters at the specified position in the string. Note that the first character is charAt (0 ). In the following example, we create a string and then use the charAt function to return the characters of each position. The Code is as follows:
JavaScript code
<Script type = "text/javascript">
Var str = "01234567, I am a long and long string ...... ";
</Script>
Test the character string charAt Method
Click the button below to test. We can find that charAt (0) is the first character of the string, that is, 0, and so on.
Alert (str. charAt (0 ))
Alert (str. charAt (2 ))
Alert (str. charAt (5 ))
The following test shows that charAt (8) is a comma. CharAt (21) is a half ellipsis. It can be seen that the ellipsis is two characters. It can also be calculated that the Chinese text in the middle is also a character.
Alert (str. charAt (8 ))
Alert (str. charAt (21 ))
JavaScript string substring Method
The substring method of the string can return the substring between two specified locations (indexes) in the string. In the following example, we create a string and use the substring function to return several substrings. For ease of observation, the string is "01234567890", so that the character at the 0 position is exactly 1 characters at the 0 position. The Code is as follows:
JavaScript code
<Script type = "text/javascript">
Var str = "01234567890 ";
</Script>
Test the substring method of a string
Click the button below to test. We can see that substring () will return "01", that is, only return position 0 and Position 1, excluding 2. It can be seen that the two parameters passed to the substring are respectively the starting character position, and the ending character position + 1.
You can click the next two buttons to test with different parameters to verify the result.
Str. substring (0, 2)
Str. substring (2, 5)
Str. substring (3,8)
The second parameter of the substring method can be omitted, indicating "until the end of the string ". For example, substring (7) returns "7890", that is, the substring from position 7 to the end of the string. From this we can see that substring (0) is the string itself.
Str. substring (4)
Str. substring (7)
Str. substring (0)
JavaScript string indexOf Method
The indexOf function of the string returns the position of the specified character or string in the string. If the character does not exist,-1 is returned. In the following example, we create a string, which is the same as the preceding example. For ease of observation, the string is "01234567890 ".
JavaScript code
<Script type = "text/javascript">
Var str = "01234567890 ";
</Script>
Test the indexOf method of a string
First, execute indexOf ('0') to check whether the string contains 0. If so, return its position. You can click the following two buttons to test.
Str. indexOf ('0 ')
Str. indexOf ('8 ')
The indexOf method is also suitable for strings. click the button below to find that indexOf ('012') returns 0, that is, the starting position of the substring '012.
Str. indexOf ('012 ')
Str. indexOf ('20140901 ')
Next, let's see if the character does not exist. If the character or string does not exist,-1 is returned.
Str. indexOf ('I ')
JavaScript string lastIndexOf Method
We can use the indexOf function to know the position of a character in the string, but indexOf returns the position where the character appears for the first time. But what if you want to know the position where a character in a string last appears? Then you need to use the lastIndexOf method of the string object.
For ease of observation, the string is "01234567890 ". We can see that the character 0 appears twice.
JavaScript code
<Script type = "text/javascript">
Var str = "01234567890 ";
</Script>
Test the lastIndexOf method of the string.
First, execute indexOf ('0'). You can see that the first occurrence of 0 is 0. Then execute lastIndexOf ('0'). We can know that the last occurrence of 0 characters is 10.
Str. indexOf ('0 ')
Str. lastIndexOf ('0 ')
JavaScript string replace method www.2cto.com
The replace method can be used to replace a specified character or substring in a string. Similarly, we still use the previously defined string "01234567890 ". Then str. replace ('1234568', 'february 11') can replace 12345 in the original string with the new February 11th.
JavaScript code
<Script type = "text/javascript">
Var str = "01234567890 ";
</Script>
Test the string replace Method
First, click the button below to observe the original string.
Str
Then, click the button below to execute the replace method. The replaced string is displayed. "01234567890" changed to "0 67890 ".
Str. replace ('20140901', 'february 11 ')
Click the button above again to view the str value. You can find that the replace method does not change the value of the original string.
JavaScript string split method
The split method can be used to split a string into an array. For example, we can separate an English sentence into a Word Array by space. Next we will create two strings to illustrate the split method.
The split method has two parameters. The first one is the delimiter. That is, what is used to split the string, for example, the comma ",", and so on. The second parameter is optional and the number of segment segments is reserved.
Split (delimiter, retain the number of segments)
JavaScript code
<Script type = "text/javascript">
Var str = "1234 ";
Var str1 = "basketball, volleyball, and table tennis ";
Var arr = str. split (""); // split all
Var arr1 = str1.split (","); // split by comma
Var arr2 = str1.split (",", 2); // split by comma and retain two segments
</Script>
First, str stores the string.
Test the string split method.
First, click the button below to observe the original string.
Str
Arr stores the str split array. From the preceding JS code, we can see that the separator is an empty string, that is, each character is separated. You can click the following button to view the array content.
Arrarr [0] arr [2]
Next let's take a look at the content of str1. Arr1 is an array separated by a comma and has three elements. In the "var arr2 = str1.split (", ", 2);" clause, the second parameter of split is used and only two segments are retained, you can click the button to view its elements.
Str1arr1arr2
Use the length of a string to limit the length of a text box
This article describes how to use the length attribute of a JavaScript string to limit the number of input texts in a text box.
In this example, The length attribute of the JS string is used to limit the length of the text in a text box. The general idea is as follows: Whenever a user enters a value in the text box (Keyboard hitting event), a function named check is triggered, and the length of Characters in the text box is obtained, check whether the length is between 5-10. If not, the corresponding warning is given. When the number of characters entered exceeds 10, the system automatically removes the longer part.
Instance JavaScript code
<Script type = "text/javascript">
Function check (){
Var str = document. getElementById ("test"). value;
If (str. length <5 ){
Update ("Enter at least five characters! ");
} Else if (str. length> 10 ){
Update ("cannot exceed 10 characters! ");
Str = str. substring (0, 10 );
Document. getElementById ("test"). value = str. substring (0, 10 );
} Else {
Update ("valid user name. ")
}
}
Function update (word ){
Document. getElementById ("feedback"). innerHTML = word;
}
</Script>
HTML code
<P>
<Label for = "test"> account: </label>
<Input type = "text" name = "test" id = "test" onkeypress = "check ()" maxlength = "15"/>
<Span id = "feedback"> </span>
</P>
Limit length text box Effect
Try to input text in the following form, and different prompts will be given later.
Account:
 
JavaScript date object setting (set) Method
JavaScript date object conversion (to) Method
JavaScript Date object application instance-Clock Code
This Code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.