The String type represents a sequence of characters composed of 0 or more 16-bit Unicode characters, i.e. a string. Strings in ECMAScript are immutable, that is, once strings are created, their values cannot be changed. To change the string saved by a variable, first destroy the original string (this process occurs in the background), and then fill the variable with another string containing the new value
Character literal/escaping sequence:
/n line break/t tab/r carriage return/b space/f page break// slash/' single quote/" double quote/xnn /unnn
String conversion: two methods
1.toString(): There is only null and undefined without this method.
Each string also has a toString() method, which returns a copy of the string. In most cases, calling the toString() method does not have to pass parameters, but when calling the toString() method of the numeric value, one parameter can be passed: the cardinality of the output value. The only thing this method needs to do is return the string representation of the corresponding value.
var num = 10;console.log(num.toString());//"10"console.log(num.toString(2));//"1010"
2.String(): Applicable to all types, follow the following rules
If the value has a toString() method, the method is called (no parameters) and the corresponding result is returned
If the value is null, return "null"
If the value is undefined, "undefined" is returned
Example
A string is an immutable and ordered sequence of 16-bit values, each character usually comes from a Unicode character set.
var i = "abcdefg";
In JavaScript strings, backslashes/ have a special purpose. Adding a character to the backslash symbol will no longer represent their literal meaning. It is worse than /n to be an escape character, which represents a newline character.
'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 a string can view the length of the string:
s.length
In addition to the length attribute, strings also provide many methods that can be called:
var s = "hello, world" //Define a string s.charAt(0) // => "h" The first character s.charAt(s.length-1) // => "d" The last character s.substring(1, 4) // => "ell" 2-4 characters s.slice(1, 4) // => "ell" Same as above s.slice(-3) // => "rld": The last three characters s.indexOf("l") // => 2 The first occurrence of character l s.lastIndexOf("l") // => 10: The last occurrence of character l s.indexOf("l", 3) // => The position s.split(",") where the character l first appears in position 3 and after position 1 // => ["hello", "world"] is split into substrings s.replace("h", "H") // => "Hello, world": Full-text character replacement s.toUpperCase() // => "HELLO WORLD"