Summary of common new methods for string in ES6, es6string

Source: Internet
Author: User

Summary of common new methods for string in ES6, es6string

This example describes how to add strings in ES6. We will share this with you for your reference. The details are as follows:

ES6 has added many methods for js, including traversal, query, and replacement. It can replace similar methods in es5. this article does not consider codePointAt and other uncommon methods.

For... Of:

Let str = "wbiokr"; for (let s of str) {console. log (s)} // result: w, B, I, o, k, r

Because es5 does not have a string-related Traversal method for js,... Of will undoubtedly be a very important role in front-end development.

Schemdes:

Let str = "wbiokr"; str. includes ('wb '); // result: truestr. includes ('wbiokr'); // result: truestr. des ('w', 1); // falsestr. des ('B', 1); // true

String. Pair des (s, I) method parameter 1 is the character to be queried (string), parameter 2 is the start position of the query, return a Boolean value, and indexOf returns the query position.

StartsWith:

Let str = "wbiokr"; str. startsWith ('wb '); // result: truestr. startsWith ('W'); // result: truestr. startsWith ('w', 1); // result: falsestr. startsWith ('B', 1); // truestr. startsWith ('kr '); // result: false

String. startsWith (s, I) method parameter 1 is the character to be queried (string), parameter 2 is the start position of the query, return a Boolean value, indicating whether the character (string) the position of the string header.

EndsWith:

Let str = "wbiokr"; str. endsWith ('kr '); // result: truestr. endsWith ('R'); // result: truestr. endsWith ('wb '); // result: falsestr. endsWith ('I', 6); // falsestr. endsWith ('I', 3); // falsestr. endsWith ('kr ', 6); // true

String. endsWith () method parameter 1 is the character to be queried (string), parameter 2 is the start position of the query, return a Boolean value, indicating whether the character (string) is located at the end of the string.

Repeat:

Let str = "wbiokr"; str. repeat (2); // wbiokrwbiokr "str is returned twice, and str does not change 'hi '. repeat (3); // "hihihi" 'hi '. repeat (2.6); // 'hihi' floating-point type parameter, which is an integer that repeats 'hi '. repeat (0); // when ''0, an empty string 'Hi' is returned '. repeat (-3) // negative number, error 'hi '. repeat (undefined) // ''undefined to 0 'hi '. repeat (null) // ''null is converted to 0'hi '. repeat (NaN) // ''NaN is converted to 0

String. repeat (num) method. The num parameter is the number of times the string is repeated. For the return values in various cases, see the code.

$ {}:

let str="wbiokr";$('#box').html('  there is a word ${str},i know it');//there is a word wbiokr,i know it

During development, jq is often used to dynamically Add sub-elements or content for elements. In the past, we used to concatenate strings to add variables. The old jq method can be replaced by ES6. Adding '$ {}' in single quotes can not only embed variables, but also maintain the code format and run js Code in.

For more information, see this topic: ECMAScript6 (ES6) getting started, JavaScript Array Operation Skills summary, JavaScript character and string operation skills summary, JavaScript data structure and algorithm skills summary, JavaScript errors and debugging skills Summary and javascript object-oriented tutorials

I hope this article will help you with ECMAScript-based programming.

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.