Unicode representation of strings

JavaScript allows the representation of a character as \uXXXX (XXXX is the Unicode code point for a character), but this representation is limited to characters with code points between \ u0000-\ uFFFF. Characters beyond this range must be represented as two double-bytes

let a = "\u0061";
a // "a"

let b = "\uD842\uDFB7";

Copy the code

ES6 improves on this by putting the code point in braces to read the character correctly

Second, the codePointAt ()

Inside JavaScript, characters are stored in UTF-16 format, and each character is fixed at 2 bytes. For characters requiring four bytes of storage (characters with Unicode code points greater than 0xFFFF), JavaScript treats them as two-character characters

The codePointAt method correctly returns the 32-bit utF-16 character code point (base 10), with the same return structure as the charCodeAt method for regular 2-byte stored characters

var ch = "𠮷";  // An unconventional character that requires 4 bytes of storage
ch.length       // 2 The string length is misjudged to be 2

var s = "𠮷 a.";    // 
s.charPointAt(0); // 134071 correctly returns the code point for the first character
s.charPointAt(1); / / 57271
s.charPointAt(2); // the decimal value of a is 97
Copy the code

CharPointAt (0) correctly handles the character “𠮷”, but because JavaScript still recognizes the length of the character as 2, the subscript must be 2 to correctly recognize subsequent characters. In this case, the subscript of character A is 2

Third, String. FromCodePoint ()

ES6 provides String.fromCodePoint(), which can recognize characters larger than 0xFFFF. This method returns the character corresponding to the codepoint

If string.fromcodePoint () has multiple arguments, they are returned as a single String

String traverser interface

ES6 adds a traverser interface to strings, allowing strings to be written by for… The of loop traverses

for… The OF traverser can recognize code points greater than 0xFFFF

Includes (), startsWith(), endsWith()

The indexOf method in JavaScript is used to determine whether one string is contained within another string

ES6 provides three new approaches:

-startswith () : Returns a Boolean value indicating whether the parameter string is at the end of the source string. -endswith () : Returns a Boolean value indicating whether the parameter string is at the end of the source stringCopy the code

All three methods support a second argument to indicate where the search should begin, endsWidth(STR, n) for the first n characters, and the other two methods for characters from the NTH position to the end of the string

Six, repeat ()

The repeat() method returns a new string, indicating that the original string is repeated n times

- If the parameter is a decimal, it will be rounded down - if the parameter is <= -1 or Infinity, an error will be reported - if the parameter is (-1, 1) open interval, repeat as 0 - NaN, null, undefined, equivalent to 0 - If the parameter is a string, it will be converted to a number firstCopy the code

PadStart (), padEnd()

ES2017 introduced string completion length. If the string length is not specified, padStart() is used for incomplete header and padEnd() is used for complete tail

PadStart () and padEnd() take two arguments, the first to specify the minimum length of the string, and the second to complete the string

- If the length of the original string is >= the specified length, the original string is returned - if the sum of the incomplete string and the original string length exceeds the minimum length, the excess number of bits is truncated - if the second argument is omitted, a space is used to complete the stringCopy the code

8. Template string

ES6 uses backquotes (‘ ‘) to define multi-line strings, or to embed variables in strings

- the template string representation multi-line string, all of the Spaces and the indentation will be preserved in the output - templates can be embedded in a string variable, expression, function call, etc., needs to be written in the ${} - value if the string is not in curly braces, transform according to the general rules for string - template string can also be nested againCopy the code