1. codePointAt
method is the simplest way to test whether a character consists of two bytes or four bytes.
1 function Is32bit (c) {2 return c.codepointat (0) > 0xFFFF; 3 }45// true6// false
2.ES6 provides methods for string instances to normalize()
unify different representations of characters into the same form, which is called Unicode normalization.
normalize
Method can accept four parameters.
-
NFC
, the default parameter, means "standard equivalent composition" (Normalization Form Canonical composition), which returns the composite character of multiple simple characters. The so-called "standard equivalence" refers to the visual and semantic equivalence.
-
NFD
, which represents "standard equivalent decomposition" (Normalization Form Canonical decomposition), which returns multiple simple characters of the composite character decomposition, on the premise of standard equivalence.
-
NFKC
, which means "compatible equivalent composition" (Normalization Form compatibility composition), which returns the composite character. The so-called "compatible equivalence" refers to the existence of semantic equivalence, but not visually equivalent, such as "DB" and "Hei XI". (This is just for example, the normalize
method does not recognize Chinese.)
-
nfkd
, which means "compatible equivalent decomposition" (Normalization Form compatibility decomposition), which returns multiple simple characters of the composite character decomposition, on the premise of compatibility equivalence.
- 1 ' \u01d1 '. Normalize () = = = ' \u004f \u030c ' . Normalize () 2 // Span style= "color: #008000;" > true
1 ' \u004f\u030c '. Normalize (' NFC '). Length // 1 2 ' \u004f\u030c '. Normalize (' NFD '). Length // 2
3.includes (), StartsWith (), EndsWith ()
- includes (): Returns a Boolean value that indicates whether the parameter string was found.
- StartsWith (): Returns a Boolean value that indicates whether the parameter string is in the header of the source string.
- EndsWith (): Returns a Boolean value that indicates whether the parameter string is at the end of the source string.
1 var s = ' Hello world! ' ; 2 3 // true 4 // true 5 // true
When using the second parameter n
, endsWith
the behavior differs from the other two methods. It is for n
the previous character, while the other two methods target from the first n
position until the end of the string.
1 var s = ' Hello world! ' ; 2 3 // true 4 // true 5 // false
4. The repeat
method returns a new string indicating that the original string is repeated n
once.
5.
JS ES6 notes on the operation of characters