A String object is used to process text (string).
1. Create a String
The code copy is as follows: var str = "Hello World";
var str1 = new String(str);
var str = String("Hello World");
All three methods are OK, but there are slight differences
2. length attribute --->Return the length of the string
3. The anchor() method is used to create HTML anchors.
What is HTML anchor?
That is <a ...>...</a>
stringObject.anchor(anchorname)
anchorname Required. Define a name for the anchor.
Copy the code as follows: var txt="Hello world!"
document.write(txt.anchor("myanchor"))
The output is:
<a name="myanchor">Hello world!</a>
Of course, what you see in the browser is Hello world!
4. The big() method is used to display strings as large fonts.
How to use:
Copy the code as follows: var str="Hello world!"
document.write(str.big())
5. The blink() method is used to display flashing strings.
How to use:
Copy the code as follows: var str="Hello world!"
document.write(str.blink())
6. The bold() method is used to display strings in bold.
Copy the code as follows: var str="Hello world!"
document.write(str.bold())
7. The charAt() method can return characters at the specified position.
stringObject.charAt(index)
index required. A number representing a position in a string, that is, the subscript of a character in the string.
Comment: The subscript of the first character in the string is 0. If the parameter index is not between 0 and string.length, the method returns an empty string.
8. The charCodeAt() method can return the Unicode encoding of the characters at the specified position. This return value is an integer between 0 - 65535.
The method charCodeAt() is similar to the operation performed by the charAt() method, except that the former returns the encoding of characters at the specified position, while the latter returns a substring of characters.
Copy the code as follows: var str="Hello world!"
document.write(str.charCodeAt(1))
Output is: 101
9. The concat() method is used to concatenate two or more strings.
stringObject.concat(stringX,stringX,...,stringX)
stringX Required. One or more string objects that will be concatenated into a string.
Tip: Please note that using the "+" operator for string concatenation operations is usually easier.
Copy the code as follows: var str1="Hello"
var str2="world!"
document.write(str1.concat(str2))
The output is:
Hello world!
10. The fontcolor() method is used to display strings according to the specified color.
stringObject.fontcolor(color)
color required. Specify font-color for strings. This value must be a color name (red), an RGB value (rgb(255,0,0)) or a hexadecimal number (#FF0000).
example:
Copy the code as follows: var str="Hello world!"
document.write(str.fontcolor("Red"))
11. The lastIndexOf() method can return the last location of the specified string value, and search from back to front at the specified location in a string.
stringObject.lastIndexOf(searchvalue,fromindex)
searchvalue Required. Specifies the string value to be retrieved.
fromdex Optional integer parameter. Specifies the location where the search starts in the string. Its legal value is 0 to stringObject.length - 1. If this parameter is omitted, the search will start from the last character of the string.
Note: The lastIndexOf() method is case sensitive!
If the string value to be retrieved does not appear, the method returns -1.
example:
Copy the code as follows: var str="Hello world!"
document.write(str.lastIndexOf("Hello") + "<br />")
document.write(str.lastIndexOf("World") + "<br />")
document.write(str.lastIndexOf("world"))
Output:
0
-1
6
12. The link() method is used to display strings as hyperlinks.
stringObject.link(url)//url Required. Specify the URL to be linked.
var str="Wulin.com"
document.write(str.link("//www.VeVB.COM"))
13. The match() method can retrieve the specified value within the string, or find a match for one or more regular expressions.
This method is similar to indexOf() and lastIndexOf(), but it returns the specified value instead of the position of the string.
stringObject.match(searchvalue)
searchvalue Required. Specifies the string value to be retrieved.
or:
stringObject.match(regexp)
regexp is required. RegExp object that specifies the pattern to match. If the parameter is not a RegExp object, you need to first pass it to the RegExp constructor and convert it to a RegExp object.
Copy the code as follows: var str="Hello world!"
document.write(str.match("world") + "<br />")
document.write(str.match("World") + "<br />")
document.write(str.match("worldd") + "<br />")
document.write(str.match("world!"))
The output is:
world
null
null
world!
14. The replace() method is used to replace some characters in a string, or to replace a substring that matches the regular expression.
stringObject.replace(regexp/substr,replacement)
regexp/substr Required. RegExp object that specifies the substring or the pattern to be replaced.
Note that if the value is a string, it is used as the direct quantity text pattern to be retrieved instead of being converted to a RegExp object first.
replacement Required. A string value. Specifies a function that replaces text or generates a replacement text.
A new string is obtained after replacing the first match or all matches of regexp with replacement.
example:
Copy the code as follows: var str="Visit Microsoft!"
document.write(str.replace(/Microsoft/, "JB51"))
The output result is:
Visit JB51!
15. The search() method is used to retrieve the specified substring in a string, or to retrieve the substring that matches the regular expression.
stringObject.search(regexp)
regexp This parameter can be a substring that needs to be retrieved in stringObject, or a RegExp object that needs to be retrieved.
Note: To perform a search that ignores uppercase and lowercase case, add the flag i.
Return value:
The start position of the first substring in stringObject that matches regexp.
Comment: If no matching substring is found, return -1.
Note: search() is case sensitive
example:
[code]var str="Visit JB51!"
document.write(str.search(/JB51/))
Output:
6
16. The slice() method can extract a part of the string and return the extracted part with a new string.
stringObject.slice(start,end)
start the start subscript of the segment to be extracted. If it is a negative number, the parameter specifies the position calculated from the end of the string. That is, -1 refers to the last character of the string, -2 refers to the second to last character, and so on.
end The subscript at the end of the segment to be extracted immediately. If this parameter is not specified, the substring to be extracted includes a string from start to the end of the original string. If the parameter is a negative number, it specifies the position from the end of the string.
Return value:
A new string. Includes all characters from the string stringObject starting (including start) to end end (excluding end).
String.slice() is similar to Array.slice()
The code copy is as follows: var str="Hello happy world!"
document.write(str.slice(6))
Output: happy world!
The code copy is as follows: var str="Hello happy world!"
document.write(str.slice(6,11))
Output: happy
17. The split() method is used to split a string into a string array.
stringObject.split(separator, howmany)
separator required. A string or regular expression that splits stringObject from where this parameter specifies.
howmany optional. This parameter specifies the maximum length of the returned array. If this parameter is set, the returned substrings will not be more than the array specified by this parameter. If this parameter is not set, the entire string will be split regardless of its length.
Note: If an empty string ("") is not a space and is used as a separator, then each character in the stringObject will be split.
example:
The code copy is as follows: var str="How are you doing today?"
document.write(str.split(" ") + "<br />")
document.write(str.split("") + "<br />")
document.write(str.split(" ",3))
Output:
How are,you,doing,today?
H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,?
How are, you
18. The substr() method can extract the specified number of characters starting from the start subscript in the string.
stringObject.substr(start,length)
start Required. The starting subscript of the substring to be extracted. Must be a numeric value. If it is a negative number, the parameter declares the position from the end of the string. That is, -1 refers to the last character in the string, -2 refers to the second to last character, and so on.
length optional. The number of characters in the substring. Must be a numeric value. If this parameter is omitted, a string from the start position of stringObject to the end is returned.
Return value:
A new string containing length characters starting from the start of stringObject (including the character referred to by start). If length is not specified, the returned string contains characters from start to the end of stringObject.
Note: ECMAscript does not standardize the method, so it is opposed to using it.
Copy the code as follows: var str="Hello world!"
document.write(str.substr(3,7))
Output:
lo world
19. The substring() method is used to extract characters in a string between two specified subscripts.
stringObject.substring(start,stop)
start Required. A non-negative integer that specifies the position of the first character of the substring to be extracted in stringObject.
stop optional. A non-negative integer is 1 more positioned in stringObject than the last character of the substring to be extracted. If this parameter is omitted, the returned substring will continue to the end of the string.
Return value
A new string whose value contains a substring of stringObject whose content is all characters from start to stop-1, with a length of stop minus start.
Note:
The substring() method returns a substring including the characters at start, but not the characters at end.
If the parameter start is equal to end, then the method returns an empty string (that is, a string of length 0). If start is larger than end, the method will swap these two parameters before extracting the substring.
Note:
Unlike slice() and substr() methods, substring() does not accept negative parameters.
example:
Copy the code as follows: var str="Hello world!"
document.write(str.substring(3,7))
Output:
lo w
20. Definition and usage
The toLowerCase() method is used to convert a string to lowercase.
stringObject.toLowerCase()//No parameter
Return value:
A new string in which all uppercase characters of stringObject are converted to lowercase characters.
21. The toUpperCase() method is used to convert a string to uppercase.
stringObject.toUpperCase()//No parameters
Return value:
A new string in which all lowercase characters of stringObject are converted to uppercase characters.
22. The indexOf() method can return the position where a specified string value first appears in the string.
stringObject.indexOf(searchvalue,fromindex)
searchvalue Required. Specifies the string value to be retrieved.
fromdex Optional integer parameter. Specifies the location where the search starts in the string. Its legal value is 0 to stringObject.length - 1. If this parameter is omitted, the search will start from the first character of the string.
illustrate:
This method will retrieve the string stringObject from beginning to end to see if it contains a substring searchvalue. The location where the search begins is at the fromdex of the string or at the beginning of the string (when fromdex is not specified). If a searchvalue is found, it returns the location where the searchvalue first appears. The character position in stringObject starts at 0.
Note: The indexOf() method is case sensitive!
example:
Copy the code as follows: var str="Hello world!"
document.write(str.indexOf("Hello") + "<br />")
document.write(str.indexOf("World") + "<br />")
document.write(str.indexOf("world"))
Output:
0
-1
6
To view more JavaScript syntax, you can follow: "JavaScript Reference Tutorial" and "JavaScript Code Style Guide". I also hope that everyone will support Wulin.com more.