JavaScript String object
String object is used to process existing character blocks.
How to use the length attribute to calculate the length of a string:
<html> <body> <script type="text/javascript"> var txt="Hello World!" document.write(txt.length) </script> </body> </html>
How to add styles to a string:
<html> <meta charset="utf-8"> <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>") </script> </body> </html>
How to use indexOf() to locate a certain element in a string The position where the specified character first appears:
<html> <meta charset="utf-8"> <body> <script type="text/javascript"> var str="Hello world!" document.write(str.indexOf("Hello") + "<br />") document.write(str.indexOf("World") + "<br />") document.write(str.indexOf("world")) </script> </body> </html>
How to use match() to find a specific character in a string, and if found, return this character:
<html> <meta charset="utf-8"> <body> <script type="text/javascript"> var str="Hello world!" document.write(str.match("world") + "<br />") document.write(str.match("World") + "<br />") document.write(str.match("worlld") + "<br />") document.write(str.match("world!")) </script> </body> </html>
How to use replace( ) method replaces some characters with other characters in a string:
<html> <meta charset="utf-8"> <body> <script type="text/javascript"> var str="Visit Microsoft!" document.write(str.replace(/Microsoft/,"PHP中文网")) </script> </body> </html>
String object
The string object is used to process existing character blocks.
Example:
The following example uses the length property of a string object to calculate the length of a string.
var txt="Hello world!"
document.write(txt.length)
The output of the above code is:
12
The following example uses the toUpperCase() method of the string object to convert the string to uppercase:
var txt="Hello world!"
document.write(txt.toUpperCase( ))
The output of the above code is:
HELLO WORLD!
Use the strong>split() function to convert the string into an array:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p id="demo">单击按钮显示数组。</p> <button onclick="myFunction()">点我</button> <script> function myFunction(){ var str="a,b,c,d,e,f"; var n=str.split(","); document.getElementById("demo").innerHTML=n[0]; } </script> </body> </html>
Special characters
In Javascript, you can use backslash (\) to insert special symbols, such as apostrophes, quotation marks and other special symbols.
View the following JavaScript code:
var txt="We are the so-called "Vikings" from the north.";
document.write(txt);
In JavaScript, use single or double quotes to start and stop strings. This means that the above string will be cut into: We are the so-called
To solve the above problem, you can use backslashes to escape the quotes:
var txt="We are the so-called \"Vikings\" from the north.";
document.write(txt);
JavaScript will output the correct text string: We are the so-called "Vikings" from the north.
The following table lists other special characters. You can use backslashes to escape special characters:
##CodeOutput
\' Single quote \" Double quote \\ Slash bar \n Line break \ r Enter \t tab\b Space
\f Page change
String properties and methods
Properties:
length
prototype
constructor
Method:
charAt()
charCodeAt()
concat()
fromCharCode()
indexOf()
lastIndexOf()
match()
replace()
search()
slice()
split()
substr()
substring()
toLowerCase()
toUpperCase()
valueOf()