JS ES6 notes on the operation of characters

Source: Internet
Author: User

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.

normalizeMethod 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.