Method attribute of the string object
String (String object)
I am not a master. I just want to write this section down for you to see. The real String object is really useful. Hey, I hope you can share it with me ..
The following is an example of the tutorial.
A String object is an encapsulation of character-type data. It can be used to manage character-type data.
However, almost all attributes and methods of string objects can be directly used in strings. Therefore, unless otherwise required, we recommend that you use strings instead of string objects.
(Except the Concat, fromcharcode, slice, and substr methods in a string object, other methods can be directly used as strings .)
It is very important to distinguish a string from an instance of a string object.CodeIn, one is to create a string a, and the other is to create a String object A2:
A = "hello" // This is a string type.
A1 = new string ("hello") // This Is A String object type, Object Type
To create a String object, use the constructor. The syntax is:
New String (value)
// Actually, I don't know what the constructor is. I just added the type string and object color to be created with new before creating a new object. Alas, my English is poor, therefore, most of my usage is better than gourd painting, and I don't know what it is.
Myname = new string ("flasher ")
This creates a new String object myname, whose initial value is "flasher"
There is only one attribute of the string object, that is, length, which is used to determine the length of the string. The syntax format is as follows. Take the newly created object as an example.
Myname. Length
For example:
Myname = new string ("flasher ")
Trace (myname. length)
Result: 7
String object method table:
Charat returns the character at the specified position of the string.
Charcodeat returns the code of the specified position character in the string.
Concat connection string
Fromcharcode is a string consisting of Characters in the current Code.
Indexof finds and returns the position value of the Child string that appears for the first time in the parent string.
Lastindexof finds and returns the last position value of the Child string in the parent string
Slice obtains the substring of a string.
Split the string and store it in the array
The substr is used to obtain the substring of a string. It is similar to the slice function, but the parameter setting method is different.
Substring: substring of the string.
Tolowercase converts uppercase letters in the string to lowercase letters
Touppercase converts lowercase letters in a string to uppercase letters.
Usage:
1. charat
The syntax is mystring. charat (index)
The index parameter is the position value of the string in mystring.
Example:
A = new string ("abcdefg ")
Trace (A. charat (1 ))
The running result is B. The String object is the same as the array object. The first character is 0. The last character is a. Length-1.
If not, consider a = new string ("abcdefg")
Compare
A = new string ("abcdefg ")
A = new string ("0123456 "),
This is a comparison. The above is the content of the object in the string, and the current is the position of each character in the object above. They are recorded from scratch, so
The first item of a = new string ("abcdefg") is B, not a. Because they start from scratch, there is also a zero item. 0 is a, and the last position value is string. Length-1.
For example, the length of a = new string ("abcdefg") is 7. but they record from scratch, but their length is calculated based on the content in it. In this example, there are seven letters, so their length is 7, but his last character's location value is 6 because they are calculated as 0123456, so the last position value is 6, so his location value can also be written as length-1.
2. charcodeat
This method returns the code of the character whose position value is index. The syntax format is:
Mystring. charcodeat (INDEX)
The returned ASCII code is not a character.
For example:
Mystring = new string ("abcdefg ")
Trace (mystring. charcodeat (1 ))
The result is 66, which is the ASCII value of character B.
I do not use this method much. I personally think this method is rarely used, so it is also vague and simple.
3. Concat
The Concat method combines the value specified in the parameter into a new string. The syntax format is as follows:
Mystring. Concat (value1, value2 ,.....)
Example:
Mystring = new string ("abcdefg ")
Newstr = mystring. Concat (1, 2, 3)
Trace (newstr)
The result is abcdefg123, which is added after the original content.
Note that this can only be used on a new string. You must use a new variable to assign the original string value. For example:
Mystring = new string ("abcdefg ")
Mystring. Concat (1, 2, 3)
Trace (mystring)
This will not show the effect. You must create a new variable to get it.
4. fromcharcode
Method to form a new string with the corresponding characters of the Code specified in the parameter and return the syntax format of the string as follows: Str = string. fromcharcode (99,100,101)
This method seems to correspond to charcodeat. I personally feel that it is not very useful. Maybe it is useless now, so I will not explain it here.
5. indexof
This method can be used to locate the first occurrence of a child string in the parent string and return the value of this position. The syntax is as follows.
Mystring. indexof (substring, [startindex])
The substring parameter is a substring to be searched. It can be an integer or a string (treated as a string when it is an integer). The startindex parameter is optional and is an integer, specifies the position of the child string to start searching, that is, starting from the nth character of the parent string. If a substring is found, the position value of the substring in the primary string is returned. If no value is found,-1 is returned. For example:
STR = "ABC123 ABC"
A = Str. indexof ("ABC ")
B = Str. indexof (26)
C = Str. indexof ("ABC", 3 );
Trace ()
Trace (B)
Trace (c)
Result: 0 ,-
The first parameter is not specified. The second parameter is startindex. Therefore, it will start with 0th characters and ABC will appear at 0th characters. Therefore, the return value is counted from the position where the first letter is found, so it is equal to 0.
The second is to find a number 26, and the second parameter is not given. Therefore, it is also found from 0th characters, but it does not contain the number 26, so if it cannot be found,-1 is returned.
The third two parameters are given, and the second parameter is 3, so he will query the ABC character, starting from the third position value of the second parameter, although ABC already exists in the first character, it is given to start from the third character bit, so it ignores all characters before the third character. At this time, the ABC character appears at the third character bit, so it returns the Location Value of the ABC found after the third position value, note. A space here is also a location value.
6. lastindexof
This method is the opposite of indexof. indexof is the first position value that appears, while lastindexof is the end position of scanning when the child string is searched in the parent string. If the substring is found, the position value of the last occurrence of the substring is returned. If the substring is not found,-1 is returned.
Column:
STR = "ABC123 ABC"
A = Str. lastindexof ("ABC ")
If the last parameter is not given, 12 is returned, and
In this case, indexof () is the number of characters from front to back, And lastindexof () is the number from back to front. I think so.
In the past, I saw someone asking me how to check that the username or password I entered on the login page contains invalid characters. Use this indexof or lastindexof, this can solve the problem of checking invalid characters on the login interface. The following is an example with a bug, but it is important to give an example to explain how to check invalid characters, instead of the real login interface, I am also afraid that when the script is too much, others will be dizzy, not others, but I will be dizzy when I read it. You can try to enter illegal characters in it to see the effect.
7. Slice
This method extracts the child string from the parent string, two parameters
Mystring. Slice (START, [end])
Start indicates the start position, and end indicates the end position.
Example:
STR = "1234567890"
A = Str. Slice (3,6)
B = Str. Slice (3)
C = Str. Slice (-8, 6)
D = Str. Slice (-5, 3)
Trace one to see the results. Are
456 4567890 3456 ""
The start point of the first a is 3, so it is extracted from 4 and ended with 6th characters, that is, 6. However, the ending point is not included in the extracted range, that is, the ending point is 6 and the corresponding character is 7, but the result is 6, because the character at the end point is not in the extraction range.
8. Split
This method divides string objects by delimiters and stores sub-strings obtained by pests into an array. The syntax format is as follows:
Mystring. Split ("delimiter", [limit])
The delimiter parameter refers to the delimiter. For example, in the "123,456,789 8" clause, it refers to the delimiter.
The limit parameter specifies the number of items to be placed in the array. this parameter is optional.
Example:
Mystring = new string ("123,456,789 ")
Myarray = mystring. Split (",", 2)
The result of trace (myarray) is 123,456, which is separated by their delimiters and the following parameter is 2. Therefore, we only put the first two items in the array, the third project, 789, is ignored.
However, if the Delimiter is not specified but a "" character is entered, it splits each character and places it in the array. For example:
STR = new string ("12345 ")
Myarray = Str. Split ("")
Trace (myarray)
The result is 1, 2, 3, 4, 5.
9. substr
This method returns the child string from the parent-child string. Syntax format:
Mystring. substr (START, [length])
The start parameter specifies the starting position value of the returned substring in the parent string. If it is a negative number, it is counted down from the end of the string, the last character is-1. The length parameter indicates the length of the substring, that is, the number of characters contained. If length is not specified, the returned substring contains all characters starting from start to the end of the character.
Example:
STR = "1234567890"
A = Str. substr (3,6)
B = Str. substr (3)
C = Str. substr (-8, 6)
Trace ()
Trace (B)
Trace (c)
The result is as follows: a = 456789 B = 4567890 c = 345678
The starting point of A is 3, that is, number 4 is extracted, and the following parameter is 6, that is, it is difficult to extract 6 characters from 4, that is, 456789,
B only gives the starting point, not the ending point, so he will extract all the characters after the starting point, that is, 4567890
The starting point of C is-8, so it should be from the back to the front, the last character is-1, the forward query,-8 is 3, so the starting point is 3, the end point is also 6, so you can check 6 characters from 3.
Maybe what I said will mislead you. In fact, the book is not so specific. The last is not the end point, but the length, the number of extracted items, if the number is 6, you can query 6 characters from the start point. If the number is not enough, ignore it.
10. substring
This method returns a substring from a string in the following format:
Mystring. subing (from,)
The from parameter indicates the starting position of the substring in the parent string. The valid range of this parameter is 0 to mystring, length-1. If from is a negative value, 0 is used. The parameter to indicates the end position of the Child string in the parent string. The value should be the position value of the ending character + 1. If this parameter does not exist, use string. length: All characters starting from the start character to the end of the string. If this parameter is a negative value, 0 is used. If the to parameter is equal to the from parameter, an empty string is returned. If the from parameter is greater than, then, this method exchanges the values of from and to and then obtains the string,
Example:
STR = "1234567890"
A = Str. substring (3,5)
B = Str. substring (5)
C = Str. substring (8, 1)
Trace ()
Trace (B)
Trace (c)
Trace ()
Result: A = 45, B = 67890, c = 2345678
The start point of A is 3, that is, the number 4, and the end point is 5, that is, the number 6, but the end point of this parameter is not in the extracted range, so it is 5, so the result is 45.
B starts at 5 and does not end, so it extracts all the items starting from 5, that is, number 6.
The starting point of C is 8, that is, 9, and the ending point is 1, that is, 2. The starting point is bigger than the ending point.
C = Str. substring (1, 8)
Then extract from 1, that is, number 2, and end with 8, that is, number 9. But the end point is not in the extracted range, so it should be 8, so it is equal
2345678
11. tolowercase
This method converts all uppercase letters of the string object mystring to lowercase and returns the converted string without changing the original string.
Mystring. tolowercase ()
Example:
STR = new string ("abcdefg ")
A = Str. tolowercase ()
Trace ()
The result is abcdefg.
12. touppercase
This method converts all lowercase letters of the string in the character object to uppercase, and returns the converted string without changing the original string. The format is as follows:
STR = new string ("abcdefg ")
A = Str. touppercase ()
Trace ()
It is equal to: abcdefg
The effect is the opposite to that of tolowercase.
Instance:
Some methods of the string object are indeed good. For example, the following example uses the combination of some special methods of the string object and the string method for the effect.