扩展JavaScript trim,trimStart,trimEnd方法

  javascript没有类型C#的Trim,TrimStart,TrimEnd方法去除空白字符或者指定的字符,需要自己扩展javascript的string类型的prototype增加string类型的Trim,TrimStart,TrimEnd方法。

  源代码如下,来源:http://code.google.com/p/vegas/

var FRL = {};
FRL.strings = {};
FRL.strings.whiteSpaceChars = 
[ 
    "\u0009" /*Horizontal tab*/ ,
    "\u000A" /*Line feed or New line*/,
    "\u000B" /*Vertical tab*/,
    "\u000C" /*Formfeed*/,
    "\u000D" /*Carriage return*/,
    "\u0020" /*Space*/,
    "\u00A0" /*Non-breaking space*/,
    "\u1680" /*Ogham space mark*/,
    "\u180E" /*Mongolian vowel separator*/,
    "\u2000" /*En quad*/,
    "\u2001" /*Em quad*/,
    "\u2002" /*En space*/,
    "\u2003" /*Em space*/,
    "\u2004" /*Three-per-em space*/,
    "\u2005" /*Four-per-em space*/,
    "\u2006" /*Six-per-em space*/,
    "\u2007" /*Figure space*/,
    "\u2008" /*Punctuation space*/,
    "\u2009" /*Thin space*/,
    "\u200A" /*Hair space*/,
    "\u200B" /*Zero width space*/,
    "\u2028" /*Line separator*/,
    "\u2029" /*Paragraph separator*/,
    "\u202F" /*Narrow no-break space*/,
    "\u205F" /*Medium mathematical space*/,
    "\u3000" /*Ideographic space*/ 
];
/**
 * 移除字符串中结尾所有的指定字符或字符串
 * <p><b>例题 :</b></p>
 * <pre class="prettyprint">
 * var str ='---hello world---';
 * str = str.trimEnd('-');//---hello world
 * </pre> 
 * @param 需要移除的字符或字符串,其类型可以说字符、字符串、数组。如果参数为null则会使用 <code class="prettyprint">whiteSpaceChars</code>数组中的值.
 * @return 移除后的字符串.
 */
String.prototype.trimEnd = function(chars /*Array*/) /*String*/
{
    if (chars == null) {
        chars = FRL.strings.whiteSpaceChars;
    }
    if (this == null || this == "") {
        return "";
    }
    var i /*int*/;
    var l /*int*/ = this.length;
    for (i = this.length - 1; (i >= 0) && (chars.indexOf(this.charAt(i)) > -1); i--) {
    }
    return this.substring(0, i + 1);
};
/**
 * 移除字符串中开始所有的指定字符或字符串
 * <p><b>例题 :</b></p>
 * <pre class="prettyprint">
 * var str ='---hello world---';
 * str = str.trimStart('-');//hello world---
 * </pre> 
 * @param 需要移除的字符或字符串,其类型可以说字符、字符串、数组。如果参数为null则会使用 <code class="prettyprint">whiteSpaceChars</code>数组中的值.
 * @return 移除后的字符串.
 */
String.prototype.trimStart = function(chars /*Array*/) /*String*/
{
    if (chars == null) {
        chars = FRL.strings.whiteSpaceChars;
    }
    if (this == null || this == "") {
        return "";
    }
    var i /*int*/;
    var l /*int*/ = this.length;
    for (i = 0; (i < l) && (chars.indexOf(this.charAt(i)) > -1); i++) {
    }
    return this.substring(i);
};
/**
 * 移除字符串中开始和结尾所有的指定字符或字符串
 * <p><b>例题 :</b></p>
 * <pre class="prettyprint">
 * var str ='---hello world---';
 * str = str.trim('-');//hello world
 * </pre> 
 * @param 需要移除的字符或字符串,其类型可以说字符、字符串、数组。如果参数为null则会使用 <code class="prettyprint">whiteSpaceChars</code>数组中的值.
 * @return 移除后的字符串.
 */
String.prototype.trim = function( chars /*Array*/ ) /*String*/ 
{
    /*
        ExtJS方案利用正则表达式
    */
    //var re = /^\s+|\s+$/g;
    //return function(){ return this.replace(re, ""); };
    
    if( chars == null )
    {
        chars = FRL.strings.whiteSpaceChars ;
    }
    
    var source = this;
    if ( source == null || source == "" )
    {
        return "" ;
    }
    
    var i /*int*/ ;
    var l /*int*/ ;
    
    ////// start
    
    l = source.length ;
    
    for( i = 0 ; (i < l) && (chars.indexOf( source.charAt( i ) ) > - 1) ; i++ )
    {
    
    }
    
    source = source.substring( i );
    
    ////// end
    
    l = source.length ;
    for( i = source.length - 1; (i >= 0) && (chars.indexOf( source.charAt( i ) ) > - 1) ; i-- )
    {
    }
    source = source.substring( 0, i + 1 ) ;
    
    ////// 
    
    return source ;
};

 

加支付宝好友偷能量挖...


评论(0)网络
阅读(550)喜欢(0)JavaScript/Ajax开发技巧