JavaScript Stri...LOGIN

JavaScript String object

Define the String object

The JavaScript String object is used to process text strings. The syntax for creating a String object is as follows:

<script language="JavaScript">
var str_object = new String( str );
var str1 = String( str );
var str2 = str;
</script>

Among the above three methods, only the first one uses the String constructor to strictly define a string object and returns an object. The second is to call the String function to convert the parameter str to the original string string and return it. The third method is to define a string variable, but it is still treated as a string object in JavaScript.

Run the following statements to know the difference:

alert( typeof str_object ); // Output object
alert( typeof str1 ); // Output string
alert ( typeof str2 ); // Output string

##String Object attribute


##Attribute                

Description


constructor A reference to the function that created the object

length The length of the string

prototype Adding properties and methods to the object


String (String) uses the length attribute length to calculate the length of the string:

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
</head>
<body>
<script>
var txt = "Hello World!";
document.write("<p>" + txt.length + "</p>");
var txt="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
document.write("<p>" + txt.length + "</p>");
</script>
</body>
</html>

String uses indexOf() to locate a specific point in the string The position where the character first appears:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<p id="p1">Click the button to locate where "locate" first occurs.</p>
<p id="p2">0</p>
<button onclick="myFunction()">点击查看</button>
<script>
function myFunction(){
var str=document.getElementById("p1").innerHTML;
var n=str.indexOf("locate");
document.getElementById("p2").innerHTML=n+1;
}
</script>
</body>
</html>

match() function is used to find a specific character in a string, and if found, returns this character.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<script>
var str="Hello world!";
document.write(str.match("world") + "<br>");
document.write(str.match("World") + "<br>");
document.write(str.match("world!"));
</script>
</body>
</html>

replace() method replaces certain characters with other characters in a string.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
</head>
<body>
<button onclick="myFunction()">点我</button>
<p id="demo">请访问 Microsoft!</p>
<script>
function myFunction() {
    var str = document.getElementById("demo").innerHTML; 
    var txt = str.replace("Microsoft","php.cn");
    document.getElementById("demo").innerHTML = txt;
}
</script>
</body>
</html>

String case conversion uses the function toUpperCase() / toLowerCase():

var txt="Hello World!"; // String

var txt1=txt .toUpperCase(); // txt1 text will be converted to uppercasevar txt2=txt.toLowerCase(); // txt2 text will be converted to lowercase

Strings use strong>split() Convert the function to 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[2];
}
</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, single or double quotes are used 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:

Code Output

\' Single quotation mark

\" Double quotation mark

\\ Slash bar

\n Line feed

\r Carriage return

\t tab

\b Space

\f Page change


Next Section
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <script type="text/javascript"> var str = "www.php.cn"; document.write( str.split(".") + "<br />" ); document.write( str.split("") + "<br />" ); document.write(str.split(".", 2)); </script> </head> <body> </body> </html>
submitReset Code
ChapterCourseware