JavaScript Common String Processing method

Source: Internet
Author: User

StringUtils provides a number of common string handling methods, including removing whitespace at both ends of a string, padding characters around a string, string formatting, string HTML encoding, and string HTML decoding.

Method Description
Stringutils.trim (str) Remove whitespace at both ends of a string
Stringutils.format (str, object ...) String formatting
Stringutils.leftpad (str, size, character) Fills the character character to the left of the string, filling the string with the length of size
Stringutils.rightpad (str, size, character) Fills the character character to the right of the string, filling the length of the string with size
Stringutils.htmlencode (str) HTML character escapes in a string
Stringutils.htmldecode (str) HTML character restore in a string


/** * Commonly used string processing method * @author [email protected] * @date 2015-08-12 * *StringUtils = ( function(){    var        //string minus blank regular expression on both sidesTrimregex =/^[\x09\x0a\x0b\x0c\x0d\x20\xa0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\ u200a\u2028\u2029\u202f\u205f\u3000]+| [\x09\x0a\x0b\x0c\x0d\x20\xa0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\ U2028\u2029\u202f\u205f\u3000]+$/g,//String formatting regular expressionsFormatregex =/\{(\d+) \}/g, Htmlencodemap = {' "':"&quot;",' & ':"&amp;","'":"& #39;",' < ':"&lt;",' > ':"&gt;"}, Htmldecodemap = {' & #39; ':"'",' &amp; ':"&",' &gt; ':">",' &lt; ':"<",' &quot; ':' "'}, Htmlencoderegex =/(&|>|<| "|") /g, Htmldecoderegex =/(&amp;|&gt;|&lt;|&quot;|& #39; |&#[0-9]{1,5};)/g;return{/** * Remove whitespace characters at both ends of the string * @param str {string} String * @return returns the processed string */Trim function(str){            returnStr.replace (Trimregex,"'); },/** * Allows value to be given in a formatted way * var cls = ' Css-class ', Text = ' content ';         * var str = Rhui.String.format (' <div class= "{0}" >{1}</div> ', CLS, text);         *//STR content is <div class= "Css-class" > Content </div> * * @param str {string} string to be formatted  * @param params ... {Object} with string {0}, {1} ... Match content * @return returns the formatted string */Format function(str) {            varI, args =arguments, Len = args.length, arr = []; for(i =1; i < Len;            i++) {Arr.push (args[i]); }returnStr.replace (Formatregex, function(M, i) {                returnArr[i];        }); },/** * Fill string Left * @param str {string} original String * @param size {number} length after filling * @param character {string} to fill the character, or null if not filled ' * @return returns the filled string */Leftpad: function(str, size, character) {            varresult ="'+ str;if(Object. Prototype.toString.call (character)!==' [Object String] ') {character ="'; } while(Result.length < size)            {result = character + result; }returnResult },/** * Fills the string to the right * @param str {string} original String * @param size {number} after filling the length * @param character {string} to fill the character, or null if not filled ' * @return returns the filled string */Rightpad: function(str, size, character) {            varresult ="'+ str;if(Object. Prototype.toString.call (character)!==' [Object String] ') {character ="'; } while(Result.length < size)            {result + = character; }returnResult },/** * to escape the HTML character in the string * @param str {String} * @return returns the escaped character */HtmlEncode: function(str) {            if(Object. Prototype.toString.call (str) = = =' [Object String] '){returnStr.replace (Htmlencoderegex, function(match, Val){                    returnHtmlencodemap[val];            }); }Else{returnStr }        },/** * Decodes the HTML characters in the string * @param str {string} * @return returns the decoded string */HtmlDecode: function(str) {            if(Object. Prototype.toString.call (str) = = =' [Object String] '){returnStr.replace (Htmldecoderegex, function(match, Val){                    returnHtmldecodemap[val];            }); }Else{returnStr }        }    };}) ();

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

JavaScript Common String Processing method

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.