JavaScript string
Overview
Strings are almost everywhere in JavaScript. When you process user input data, when reading or setting DOM objects, properties, when manipulating cookies, and of course much more…. The core part of JavaScript provides a set of properties and methods for common string operations, such as splitting strings, changing the case of strings, operating substrings, etc.
Creation of string
Creation There are several methods for a string. The simplest is to enclose a set of characters in quotes, which can be assigned to a string variable.
var myStr = "Hello, String!";
You can use double quotes or single quotes to include the string, but be aware that the pair of quotes that delimit the string must be the same , cannot be mixed.
A statement like var myString = "Fluffy is a pretty cat.'; is illegal.
Allows the use of two kinds of quotation marks, making certain operations simple, such as embedding one type into another:
document.write("<img src='img/logo.jpg' height='30' width='100' alt='Logo'>");
String splicing
is very simple, just use a "+" to join the two strings" Add ":
var longString = "One piece " + "plus one more piece.";
To accumulate multiple strings into one String, you can also use the "+=" operator:
var result = "";
result += "My name is Anders"
result += " and my age is 25";
To add a newline character to the string, you need to use the escape character "\n":
var confirmString = "You did not enter a response to the last " +
"question.\n\nSubmit form anyway?";
var confirmValue = confirm(confirmString);
String length
You can use the built-in property length to calculate the length of a 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="what are you doing ?"; document.write("<p>" + txt.length + "</p>"); </script> </body> </html>
Special characters
In JavaScript, strings are written in single or double quotes.
Because of this, the following example JavaScript cannot be parsed: x
"We are the so-called "Vikings" from the north."
The string "We are the so-called " is truncated.
How to solve the above problems? You can use backslash (\) to escape double quotes in the "Vikings" string, as follows:
"We are the so-called \"Vikings\" from the north."
Backslash is an escape character. Escape characters convert special characters into string characters:
The escape character (\) can be used to escape apostrophes, newlines, quotation marks, and other special characters.
The following table lists the special characters that can be escaped using escape characters in strings:
##CodeOutput
\' Single quote \" Double quote \\ Backslash \n Newline \r Carriage return \t tab (tab character) \b backspace character \f form feed character<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p id="demo"></p> <script> var x = "John"; // x是一个字符串 var y = new String("John"); // y是一个对象 document.getElementById("demo").innerHTML =typeof x + " " + typeof y; </script> </body> </html>
Note:
Do not create String objects. It will slow down execution and may have other side effectsString properties and methods
Primitive value strings, such as "John", have no properties and methods (because they are not objects).
Primitive values can use JavaScript properties and methods, because JavaScript can treat primitive values as objects when executing methods and properties.