JavaScript string
JavaScript strings are used to store and process text.
JavaScript String
String can store a series of characters, such as "liu qi".
The string can be any character inserted into the quotes. You can use single or double quotes:
var carname = "double quote";
var carname = 'single quote';
You can use an index position to access each character in the string:
var character = carname[7];
The index of the string starts from 0, which means that the first 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:
var answer = "It's alright";
var answer = "He is called 'Johnny'";
var answer = 'He is called "Johnny"';
You can also add escape characters to the string to use quotes:
Example
php中文网(php.cn)
Run the program and try it
String length
You can use the built-in property length to calculate the length of a string:
Example
php中文网(php.cn)
Run the program to try it out
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:
Code | Output |
---|---|
\' | Single quotes |
\" | Double quotes |
\\ | Backslash |
Line feed | |
Enter | |
tab(tab character) | |
Backspace character | |
Form feed character |
Strings can be objectsNormally, JavaScript strings are primitive values, It can be created using characters: var firstName = "John"
But we can also use the new keyword to define the string as an object: var firstName = new String("John")
php中文网(php.cn)
Run the program and try it
Note: Do not create a String object. It will slow down execution and may have other side effects:
Run the program and try itphp中文网(php.cn) === 为绝对相等,即数据类型与值都必须相等。
=== is absolutely equal, that is, the data type and value must be equal
##String 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 convert primitive values when executing methods and properties. As an object. String methods we will introduce in the following chapters##String properties
##Property
String method
constructor | Returns a function that creates a string property |
---|---|
length | Returns the length of the string |
prototype | Allows you to add properties and methods to the 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() | Connect two Or multiple strings, return the concatenated string |
fromCharCode() | Convert Unicode to a string |
indexOf() | Returns the position where the specified character appears for the first time in the string |
lastIndexOf() | Returns the position where the specified character appears in the string The position of the last occurrence of a character |
localeCompare() | Compares two strings in locale-specific order |
match() | Find a match for one or more regular expressions |
replace() | Replace the substring that matches the regular expression |
search() | Retrieve values that match 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 the two specified index numbers in the string |
toLocaleLowerCase() | Convert the string to lowercase according to the locale of the host, Only a few languages (such as Turkish) have local-specific case mapping |
toLocaleUpperCase() | Convert a string to uppercase according to the host's locale, only Several languages (such as Turkish) have local-specific case mapping |
toLowerCase() | Convert a 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 value of a string object Original value |