Detailed description of the JavaScript string number left complement, right complement, take a fixed length, intercept Extended Function Code

Source: Internet
Author: User
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!

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.