JavaScript string
JavaScript strings are used to store and process text.
JavaScript String
A string can store a sequence of characters, such as "John Doe".
The string can be any character inserted into the quotes. You can use single or double quotes:
Example
var carname = 'Volvo XC60';
You can use the index position to access each character in the string:
Example
The index of the string starts from 0, which means that the first character index value is [0], the second is [1], and so on.
You can use quotation marks in the string. The quotation marks in the string should not be the same as the quotation marks in the string:
Example
var answer = "He is called 'Johnny'";
var answer = 'He is called "Johnny"';
You can also add escapes to the string characters to use quotation marks:
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p id="demo"></p> <script> var x = 'It\'s alright'; var y = "He is called \"Johnny\""; document.getElementById("demo").innerHTML = x + "<br>" + y; </script> </body> </html>
Run Instance»
Click the "Run Instance" button to view the online instance
String length
You can use the built-in property length to calculate the length of a string:
Example
<!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>
Run instance»
Click the "Run instance" button to view the online instance
Special characters
In JavaScript, strings are written in single quotes or double quotes.
Because of this, the following example JavaScript cannot be parsed: x
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:
The backslash is a 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:
Code | Output |
---|---|
\' | Single quotes |
\" | Double quotes |
\\ | Backslash |
\n | Newline |
Enter | |
tab(tab) | |
Backspace character | |
Form feed character |
Normally, JavaScript strings are primitive values and can be created using characters:
var firstName = "John"But we can also use the new keyword to add characters A string is defined as an object:
var firstName = new String("John")Instance
<!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>
Run Instance»Click the "Run Instance" button See online examples
Do not create String objects. It slows down execution and may have other side effects: |
---|
<!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 = x===y;
</script>
<p>=== 为绝对相等,即数据类型与值都必须相等。</p>
</body>
</html>
Run Example»Click the "Run Example" button to view the online example=== is absolutely equal, that is, the data type and value must be equal.
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.
String methods we will introduce in the next chapter.String attribute
Description | |
---|---|
Returns the function that creates the string property | |
Returns the length of the string | |
Allows you to add properties and methods to an object |
Method | Description |
---|---|
charAt() | Returns the character at the specified index position |
charCodeAt() | Returns the Unicode value of the character at the specified index position |
concat() | Concatenate two or more strings and return the concatenated string |
fromCharCode() | Convert Unicode to characters String |
indexOf() | Returns the position where the specified character appears for the first time in the string |
lastIndexOf() | Returns the position of the last occurrence of the specified character in the string |
localeCompare() | Compares two strings in a local-specific order |
match() | Find a match for one or more regular expressions |
replace() | Replace the substring that matches the regular expression |
search() | Retrieve the value that matches the regular expression |
slice() | Extract a fragment of the string and return the extracted part in a new string |
split() | Split the string into an array of substrings |
substr() | Extract the specified number of characters from the string from the starting index number |
substring() | Extract the characters between two specified index numbers in the string |
toLocaleLowerCase() | Convert a string to lowercase based on the host's locale. Only a few languages (such as Turkish) have local-specific case mapping |
toLocaleUpperCase() | according The host's locale converts the string to uppercase, and only a few languages (such as Turkish) have local-specific case mapping |
toLowerCase() | Convert the string to lowercase |
toString() | Return the string object value |
toUpperCase() | Convert the string to uppercase |
trim() | Remove the leading and trailing blanks of the string |
valueOf() | Return the original value of a string object |