New padStart Magical API string of ES6

ES6 used in everyday development is very common, in which the string processing capabilities strengthened, added several useful API, for example: trimStart, trimEnd, pasStart, padEnd, stratsWith, endsWith, include, repeat ... etc. Wait. Today we want to discuss is pasStart / pasEnd.

Once upon a time, when we deal with time formatting issues often encounter "fill zero problems", simply put, is under 100,000, a bit 0s, so there is the following code:

const seconds = str > 10 ? str : `0${str}`;

Such seems to be no problem, but since has ES6 happens, we have a more elegant wording, apply their knowledge, we can use ES6 new string API-padStart:

const seconds = str.padStart(2, "0");

Explain the parameters of usage padStar

The first parameter represents the maximum length of the string (containing string length string length before treatment + filled)

The second parameter indicates a string needs to be filled, if the parameter is not passed, the default fill spaces

 

Well, today share little skill you get it? Comments welcome exchange!

Guess you like

Origin www.cnblogs.com/zt123123/p/12515946.html