This article mainly introduces the JavaScript string, which contains the left-side, right-side, fixed-length, and truncation extended function code, if you need it, refer to the next article, which mainly introduces the left-complement, right-complement, fixed-length, and truncation Extension function code of JavaScript strings. If you need it, refer
Sometimes we can use this to add zero for less than two dates.
This is something everyone should understand. The Code is as follows:
String.prototype.padLeft =Number.prototype.padLeft = function(total, pad) { return (Array(total).join(pad || 0) + this).slice(-total);}
Test code:
// Complement "X ". padLeft (5, "Y"); // return: YYYYX (50 ). padLeft (5); // return: 00050var x = 1; x. padLeft (2); // return: 01 // capture (123 ). padLeft (2); // return value: 23
More code
/*** Obtain the sub-string ** @ param oriStr original string * @ param beginIndex get the start position of the sub-string * @ param len Get the length of the sub-string * @ return sub-string */public string subString (String oriStr, int beginIndex, int len) {int strlen = oriStr. length (); beginIndex = beginIndex-1; if (strlen <= beginIndex) {System. out. println ("out of" + oriStr + "'s length, please recheck! ");} Else if (strlen <= beginIndex + len) {str = oriStr. substring (beginIndex);} else {str = oriStr. substring (beginIndex, beginIndex + len);} return str;}/*** right complement, left-aligned * @ param oriStr original String * @ param len target String Length * @ param alexin complement character * @ return target String */public String padRight (String oriStr, int len, char alexin) {int strlen = oriStr. length (); if (strlen <len) {for (int I = 0; I
The above is a detailed description of the JavaScript string code of the left-side, right-side, fixed-length, and truncated Extended Function Code. For more information, see other related articles in the first PHP community!