The string type represents a sequence of characters consisting of 0 or more 16-bit Unicode characters, that is, a string. Strings in ECMAScript are immutable, that is, once a string is created, their values cannot be changed. To change the string that a variable holds, first destroy the original string (which occurs in the background), and then populate the variable with another string containing the new value
Character literal/escape sequence:
\ n
-linefeed \
\ r
-tab-return \b Space
\f the page break \
slash
\ ' single quote
\ ' Double quote
\xnn
\unnn
String Conversions: Two methods
1.toString (): Only null and undefined do not have this method.
Each string also has a ToString () method that returns a copy of the string. In most cases, calling the ToString () method does not have to pass arguments, but when you call the ToString () method of a value, you can pass a parameter: the cardinality of the output value. The only way to do this is to return a string representation of the corresponding value.
var num = ten;
Console.log (Num.tostring ());//"Ten"
Console.log (num.tostring (2));//"1010"
2.String (): Applicable to all types, follow these rules
If the value has the ToString () method, call the method (without parameters) and return the corresponding result
Returns "NULL" if the value is null
Returns "Undefined" if the value is undefined
Example
A string is a set of immutable ordered sequences of 16-bit values, each of which is usually from a Unicode character set.
In a JavaScript string, a backslash \ has a special purpose, and a backslash, followed by a character, no longer denotes their literal meaning, or an escape character, which represents a line break.
' You\ ' re right, it can\ ' t be a quote '
One of the built-in features of JavaScript is string concatenation:
msg = "Hello," + "world";
The length property of the string allows you to view the lengths of the string:
In addition to the length property, the string also provides a number of methods that can be invoked:
var s = "Hello, world"//define a string
s.charat (0) //=> "H" first character
S.charat (s.length-1)//=> "D" last character
s . substring (1, 4)//=> "ell" 第2-4个 character
S.slice (1,4)//=> "ell" ditto
S.slice ( -3)//=> "Rld": last three characters
s.in Dexof ("L")//=> 2 character L first appeared position
S.lastindexof ("L")//=> 10: Character L last occurrence
s.indexof ("L", 3)//=> at position 3 and after first The position of the character L is
s.split (",")//=> ["Hello", "world"] is divided into substrings
s.replace ("H", "H")//=> "Hello, World": Full character replacement
s.touppercase ()//=> "HELLO World"