In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to use ES6 string". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
string duplication
repeat(): Returns a new string, indicating that the string is repeated a specified number of times.
console.log("Hello,".repeat(2)); // "Hello,Hello,"
Rounding down if argument is decimal
console.log("Hello,".repeat(3.2)); // "Hello,Hello,Hello,"
If the argument is a decimal number between 0 and-1, rounding is performed, and the decimal number between 0 and-1 is rounded to-0, which is equivalent to repeat zero times.
console.log("Hello,".repeat(-0.5)); // ""
If the parameter is NaN, it is equivalent to repeat zero times.
console.log("Hello,".repeat(NaN)); // ""
If the argument is negative or Infinity, an error is 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 head (left) with a parameter string.
padEnd: Returns a new string, indicating that the original string is completed from the end (right) with the parameter string.
The above two methods take two parameters, the first parameter specifying the minimum length of the string to be generated, and the second parameter specifying the string 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 the completion string is greater than the specified length, truncate the completion string that exceeds the number of digits:
console.log("hello".padEnd(10,",world! ")); // "hello,worl"
Often used to complete digits:
console.log("123".padStart(10,"0")); // "0000000123"
template strings
Template string is equivalent to the enhanced version of the string, with backquotes `, in addition to ordinary strings, can also be used to define multi-line strings, but also can be added to the string variables and expressions.
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?
String insertion variables and expressions.
Variable names are written in ${}, where JavaScript expressions can be placed.
let name = "Mike";let age = 27;let 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.
Calling a function in a string:
function f(){ return "have fun! ";}let string2= `Game start,${f()}`;console.log(string2); // Game start,have fun!
attention points
alert`Hello world!`;// Equivalent to alert ('Hello world! ');
When template strings have variables, template string parameters are processed into multiple parameters.
function f(stringArr,... values){ let result = ""; for(let i=0;i
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.