JavaScript strings are used to store and process text.
JavaScript string
JavaScript strings are used to store and process text.
JavaScript string
A string can store a series of characters, such as "John Doe ".
A string can be any character inserted into quotation marks. You can use single or double quotation marks:
Instance
var carname = "Volvo XC60";var carname = 'Volvo XC60';
You can use the index location to access each character in the string:
Instance
var character = carname[7];
The index of the string starts from 0, which means that the index value of the first character is [0], the second is [1], and so on.
You can use quotation marks in a string. The quotation marks in a string should not be the same as those in a string:
Instance
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 quotation marks:
Instance
var answer = 'It's alright';var answer = "He is called "Johnny"";
String Length
You can use the built-in attribute length to calculate the length of a string:
Instance
var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";var sln = txt.length;
Special characters
In JavaScript, strings are written in single quotes or double quotation marks.
As a result, the following example of JavaScript cannot be parsed: x
"We are the so-called "Vikings" from the north."
The string "We are the so-called" is truncated.
How can we solve the above problems? You can use a backslash (\) to escape the double quotation marks in the "Vikings" string, as shown below:
"We are the so-called \"Vikings\" from the north."
A backslash is an escape character. Escape characters convert special characters to string characters:
Escape Character (\) can be used to escape character, line feed, 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 quotation marks
\ Backslash
\ N line feed
\ R press ENTER
\ T tab (tab)
\ B Return character
\ F page feed
A string can be an object.
Generally, JavaScript strings are original values and can be created using characters: var firstName = "John"
However, we can also use the new keyword to define a String as an object: var firstName = new String ("John ")
Instance
var x = "John";var y = new String("John");typeof x // returns Stringtypeof y // returns Object
Do not create a String object. It slows down execution and may produce other side effects:
Instance
var x = "John"; var y = new String("John");(x === y) // is false because x is a string and y is an object.
String attributes and Methods
The original value string, such as "John", has no attributes and methods (because they are not objects ).
The original values can use JavaScript attributes and methods, because JavaScript can treat the original values as objects when executing methods and properties.
We will introduce the string method in the next section.
String attribute
Attribute
Description
Constructor returns the function used to create the string attribute.
Length returns the length of the string.
Prototype allows you to add attributes and methods to an object
String Method
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 () connects two or more strings and returns the connected strings.
FromCharCode () converts a character to a Unicode Value
IndexOf () returns the position of the first occurrence of a specified character in a string.
LastIndexOf () returns the position of the last occurrence of a specified character in a string.
LocaleCompare () compares two strings in a specific local order.
Match () finds matching of one or more regular expressions
Replace () replaces the substring that matches the regular expression.
Search () searches for values that match Regular Expressions
Slice () extracts the string segment and returns the extracted part in the new string.
Split () Splits a string into a substring array.
Substr () extracts a specified number of characters from the start index number.
Substring () extracts the characters between two specified index numbers in the string.
ToLocaleLowerCase () converts strings to lowercase Based on the host's language environment. Only a few languages (such as Turkish) have local-specific case ing.
ToLocaleUpperCase () converts strings to uppercase Based on the host's language environment. Only a few languages (such as Turkish) have local-specific case ing.
ToLowerCase () converts string to small
ToString () returns the value of a string object.
ToUpperCase () converts string to uppercase
Trim () removes the white space at the beginning and end of the string.
ValueOf () returns the original value of a string object.
The above is the content of the JavaScript string in the [JavaScript tutorial]. For more information, see the PHP Chinese website (www.php1.cn )!