String string object
String String object
(1)String attributes
This object has only one attribute, length, which represents the length of the string. The number of characters, including all spaces and symbols:
var test_var = "I love You!"; document.write(test_var.length);
The displayed result is "11" because the string length also counts symbols and spaces:
Methods to access string objects:
Use the toUpperCase() method of the String object to convert the string lowercase letters to uppercase:
var mystr="Hello world!"; var mynum=mystr.toUpperCase();
The above code is executed Finally, the value of mynum is: HELLO WORLD!
(2)String method
String The object has a total of 19 built-in methods , mainly including functions such as the display of strings on the page, font size, font color, character search, and character case conversion. The following are some commonly used ones:
charAt(n) : Returns the nth single character of the string. (Counting from 0)
charCodeAt(n): Returns the ASCII code of a single character at the nth position of the string.
indexOf() : Usage: string_1.indexOf(string_2,n); Start searching from the nth position of string string_1, search for string_2, and return the found position, if not found , then -1 is returned, n can be left blank, and the search starts from the 0th position by default.
lastIndexOf(): Similar to indexOf(), but starting from the back.
split('separator') : Separate the string according to the specified delimiter and return an array, for example: '1&2&345&678'.split('&'); Return an array :1,2,345,678.
substring(n,m) : Returns the substring of the original string from position n to position m.
substr(n,x) : Returns the substring of the original string starting at position n and having a length of x.
toLowerCase(): Returns a string in which all uppercase letters of the original string are changed to lowercase.
toUpperCase() : Returns a string in which all lowercase letters of the original string are changed to uppercase.
Calculate the length of the string
<html> <body> <script type="text/javascript"> var txt="Hello World!" document.write(txt.length) </script> </body> </html>
Add styles to the string
<html> <body> <script type="text/javascript"> var txt="Hello World!" document.write("<p>Big: " + txt.big() + "</p>") document.write("<p>Small: " + txt.small() + "</p>") document.write("<p>Bold: " + txt.bold() + "</p>") document.write("<p>Italic: " + txt.italics() + "</p>") document.write("<p>Blink: " + txt.blink() + " (does not work in IE)</p>") document.write("<p>Fixed: " + txt.fixed() + "</p>") document.write("<p>Strike: " + txt.strike() + "</p>") document.write("<p>Fontcolor: " + txt.fontcolor("Red") + "</p>") document.write("<p>Fontsize: " + txt.fontsize(16) + "</p>") document.write("<p>Lowercase: " + txt.toLowerCase() + "</p>") document.write("<p>Uppercase: " + txt.toUpperCase() + "</p>") document.write("<p>Subscript: " + txt.sub() + "</p>") document.write("<p>Superscript: " + txt.sup() + "</p>") document.write("<p>Link: " + txt.link("//m.sbmmt.com") + "</p>") </script> </body> </html>