Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use ES6 string

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to use ES6 string", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use ES6 string" this article.

ES6 is ECMAScript 6 is the standard for the new version of the JavaScript language. Although it has been updated to ES7, many browsers still don't know where the ES7 syntax is, and the standard is still being updated, but the department's websites all point out the syntax of ES6. At present, ES6 is also the most widely used and latest javaScript language standard.

String repetition

Repeat (): returns a new string, indicating that the string is repeated a specified number of times.

Console.log ("Hello," .repeat (2)); / / "Hello,Hello,"

If the parameter is a decimal, round it down

Console.log ("Hello," .repeat (3.2); / / "Hello,Hello,Hello,"

If the parameter is a decimal between 0 and-1, it will be rounded, and the decimal between 0 and-1 will be rounded to-0, which is equivalent to repeat zero.

Console.log ("Hello," .repeat (- 0.5)); / / ""

If the parameter is NaN, it is equivalent to repeat zero.

Console.log ("Hello," .repeat (NaN)); / / ""

If the parameter is negative or Infinity, an error will be reported:

Console.log ("Hello," .repeat (- 1)); / / RangeError: Invalid count valueconsole.log ("Hello," .repeat (Infinity)); / / RangeError: Invalid count value

If the parameter passed in is a string, the string is converted to a number first

Console.log ("Hello," .repeat ("hh")); / / "" console.log ("Hello," .repeat ("2")); / / "Hello,Hello," string completion

PadStart: returns a new string, indicating that the original string is completed from the header (left) with a parameter string. PadEnd: returns a new string that completes the original string from the tail (right) with a parameter string. The above two methods accept two arguments, the first of which specifies the minimum length of the generated string, and the second of which is the string used to complete. If the second parameter is not specified, it is filled with spaces by default.

Console.log ("h" .padStart (5, "o")); / / "ooooh" console.log ("h" .padEnd (5, "o")); / / "hoooo" console.log ("h" .padStart (5)); / / "h"

If the specified length is less than or equal to the length of the original string, the original string is returned:

Console.log ("hello" .padStart (5, "A")); / / "hello"

If the length of the original string plus completion string is greater than the specified length, the completion string that exceeds the number of digits is truncated:

Console.log ("hello" .padEnd (10, ", world!"); / / "hello,worl"

Often used to complete digits:

Console.log ("123" .padStart (10," 0 ")); / /" 0000000123 "template string

The template string is equivalent to the enhanced version of the string, with backquotes `, in addition to being a normal string, it can also be used to define multi-line strings, and variables and expressions can be added to the string.

Basic usage

Ordinary string

Let string = `Hello'\ n'world`; console.log (string); / / "Hello'// 'world"

Multiline string:

Let string1 = `Hey,can you stop angry now? `; console.log (string1); / / Hey,// can you stop angry now?

The string inserts variables and expressions.

The variable name is written in, and the JavaScript expression can be put in {}.

Let name = "Mike"; let age = 27 info info = `My Name is ${name}, I am ${age+1} years old next year.`console.log (info); / / My Name is Mike,I am 28 years old next year.

Call the function in the string:

Function f () {return "have fun!";} let string2= `Game start,$ {f ()} `; console.log (string2); / / Game start,have fun!

Pay attention to the main points

Alert`Hello world! `; / / equivalent to alert ('Hello worldview')

When there are variables in the template string, the template string parameters are processed into multiple parameters.

Function f (stringArr,...values) {let result = ""; for (let iTunes [I]) {result + = values [I];} return result;} let name = 'Mike';let age = 27; f`My Name is ${name}, I am ${age+1} years old next year.`; / / "My Name is Mike,I am 28 years old next year." f`My Name is ${name}, I am ${age+1} years old next year.` / / equivalent to f (['My Name is',',I am', 'years old next year.'],' Mike',28)

Filter HTML strings to prevent users from entering malicious content.

Function f (stringArr,...values) {let result = "; for (let iTunes [I]) {result + = String (values [I]). Replace (/ & / g," & ") .replace (/ > / g," > "). Replace (/ > / g," > ");}} return result;} name ='; f`Hi, ${name} I would like send you some message.` / / Hi, .I would like send you some message.

Internationalization processing (conversion of multiple languages)

I18n`Hello ${name}, you are visitor number ${visitorNumber}. `; / / Hello * *, you are the first visitor. These are all the contents of the article "how to use ES6 string". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report