First, the String object introduction
The string object, like any other language, is used for processing strings.
Strings are estimated to be the most used in real-world development, so the use of strings can also affect the speed of project development.
The syntax is simple: var str=new String ("strvalue"), but we basically use the direct assignment method, var str= "strvalue";
Description
Although both of the above methods can create the string we want, a string object created with a direct assignment is different from the processing of a string object created with the new operator. All strings share a common global string object. If you add a property to a string, it is available to all string standards.
looking at the above sentence may be a lot of people are dizzy, do not understand what the meaning of the following to give an example of the next may be very good understanding.
(1) Direct Assignment method
Code var str1,str2;str1= " This is a string "; str2= " This is also a string "; str1.test=10;
Above str1 set a test property, then STR2 is also defined by the Test property, in fact, all the strings defined after str1 are defined by the test property.
(2) New Creation method
Code var str1,str2;str1=Newstring(" This is a string "); str2=new stirng (" This is another string "); str1.test=10;
In this case, STR2 does not define the test property. Because each string object declared with the new string () has its own set of members.
Ii. common methods of String objects
(1) charAt (index) method, which returns the character at the specified index position. Where the value of index is 0 to the length of the string minus 1, and if this range is exceeded, a null character is returned.
Code function charAt01 (str,index) { var s=str.charat (index); return s;}
(2) concat (string) method, returns the concatenated string, the result of which can be two strings or more string concatenated results. str=str1+str2+str3+ ... "" +STRN; if there are arguments that are not strings, they are converted to strings before they are concatenated.
Code function concat01 (STR1,STR2) { return str1.concat (STR2)}
(3) IndexOf () method, returns the position of the first occurrence of a character in a string, with a valid value of 0 to the length of the searched string minus 1. The method returns an integer value that returns 1, indicating that no string was found.
Code function indexOf01 (str) { var str1= "abcdefg"; return str1.indexof (str);}
The IndexOf () method actually has a parameter, that is, the location to start looking for, that is, the notation is Str1.indexof (str2,startindex), if the startIndex is negative, then it is considered to be 0, If the startindex is greater than the length of str1, then it is considered the largest lookup index.
(4) LastIndexOf () returns the last occurrence of the found string. Indicates the starting position of a substring in a string object. If no substring is found, 1 is returned.
Code function latIndexOf01 (str) { var s= "abcdefg"; return s.lastindexof (str);}
The same lastIndexOf () method also has a parameter, which is also the location to start looking for, that is, Str1.lastindexof (Str2,startindex), if the startIndex is negative, then it is considered to be 0, If the startindex is greater than the length of str1, then it is considered the largest lookup index.
It is worth noting that: IndexOf is looking from left to right, LastIndexOf is looking from right to left, otherwise the two need not exist at the same time.
(5) Replace method, which returns the copy of the string following the literal substitution based on the regular expression, with the syntaxstr.replace(rgExp, replaceText),
str
Required option. A string object or string literal to perform the substitution. The string is not modified by the replace method.
Rgexp
Required option. is a regular expression object that contains a regular expression pattern or an available flag. It can also be a String object or literal. If rgexp is not a regular Expression object, it will be converted to a string, and the exact lookup is done, and do not attempt to convert the string to a regular expression.
ReplaceText
Required option. is a string object or string literal, and the position in each matching rgexp in stringobj is replaced with the text contained by the object. In Jscript 5.5 or later, thereplacetext parameter can also be a function that returns alternate text.
Code function Replacedemo () { var r, re; var ss = "The man hits the ball with thebat.\n"; SS + = "While the fielder caught the ball with theglove."; re =/the/g; r = Ss.replace (Re, "A"); return (r); }
(6) The Substring method returns the string at the specified position, the syntax is substring (startindex,endindex), the StartIndex start position, the EndIndex end position, and the index starts at 0.
Description
The substring method returns a string containing a substring from start to last (without end ).
The substring method uses the smaller values from start and end as the starting point for the substring. For example, Strvar. substring (0, 3) and Strvar. substring (3, 0) will return the same substring.
If start or end is NaN or negative, replace it with 0.
The length of the substring equals the absolute value of the difference between start and end . For example, in Strvar. substring (0, 3) and Strvar. The length of the substring returned by substring (3, 0) is 3.
Code function Substringdemo () { var ss; var s = "Therain in Spain falls mainly in the plain."; SS = S.substring (n, +); return (ss); }
Of course there are other methods of string objects, but those methods are rarely used here to say, you can refer to the Help document.
It should be added that a single method is rarely used in development, but that many methods are used together, such as a news page URL of http://www.hello.com. Id=1, I need to get the URL, do not need the previous HTTP and the following ID, then we can do this:
Code function Test () { var str= "http://www.hello.com? Id=1"; return str.substring (Str.indexof ("//") +1,str.lastindexof ("?") -1);}
The above is filled in the Insert code block, because the time relationship has not been verified, if there is an error, please point out, thank you
JavaScript String Object