扩展js string 方法

eg :

 
 
//扩展strWith方法
String.prototype.startWith = function (str) {
    if (str == null || str == "" || this.length == 0 || str.length > this .length)
        return false ;
    if ( this .substr(0 , str.length) == str)
        return true ;
    else
        return false ;
    return true ;
}
 
//扩展endWith方法
String.prototype.endWith = function (str) {
    if (str == null || str == "" || this.length == 0 || str.length > this.length)
        return false ;
    if ( this.substring(this .length - str.length) == str)
        return true ;
    else
        return false ;
    return true;
}
 
//扩展trim方法
String.prototype.trim = function () {
    return this.replace(/(^\s*)|(\s*$)/g, "" );
}
String.prototype.ltrim = function () {
    return this.replace(/(^\s*)/g, "" );
}
String.prototype.rtrim = function () {
    return this.replace(/(\s*$)/g, "" );
}
  
posted @ 2014-04-18 15:19  lichunlin  阅读(528)  评论(0编辑  收藏  举报
春霖