JS字符串操作方法擴展string-extension.js

字符串操作方法擴展

/*
 * 去掉字符串前後空格
 */
String.prototype.trim = function () {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}
 
/*
 * 把字符串轉換爲json
 */
String.prototype.toJson = function () {
 
    return $.parseJSON(this);
 
    //return JSON.parse(this);
}
 
/*
 * 把字符串轉換爲人民幣的顯示方式(如:¥123,456,78.12)
 * @param decimalCount - 保留小數的位數
 */
String.prototype.toMoneyString = function (decimalCount) {
    var result = "";
    var arr = this.split('.');
    var i;
    for (i = arr[0].length - 3;i>0;i-=3){
        result = "," + arr[0].substr(i, 3) + result;
    }
    result = "¥" + arr[0].substring(0, i+3) + result;
    if (arr.length > 1) {
 
        if (decimalCount > arr[1].length)
        {
            result += "." + arr[1];
            for (var j = 0; j < decimalCount - arr[1].length; j++)
            {
                result += "0";
            }
        }
        else
        {
            result += "." + arr[1].substring(0, decimalCount);
        }     
    }
    return result;
}
 
 
/*
 * 獲取字符串的字節數
 */
String.prototype.getByteCount=function(){
    return this.replace(/[^\x00-\xFF]/g, '**').length;
}
 
 
/*
 * 切割字符串
 * @param length - 切割的長度(按字符的字節數計算,比如一箇中文算2個長度丶英文算1個長度)  
 */
String.prototype.cutString = function (length) {
    var strCount = this.getByteCount();
    if (strCount <= length) {
        return str;
    }
 
    var sum = 0;
    var cutString = "";
    for (i = 0; i < this.length; i++) {
        var c = this.charAt(i);
        sum += c.getByteCount();
        cutString += c;
        if (sum >= length) {
            break;
        }
    }
    return cutString;
}
 
 
/*
 * 移除字符串(如果移除的字符下標越界[startIndex+count>字符串自身長度],則不進行移除操作)
 * @param startIndex - 開始移除的下標
 * @param count - 要移除的字符個數
 */
String.prototype.remove = function (startIndex, count) {
    if (startIndex+count >= this.length) {
        return this;
    }
    var str1 = this.substring(0, startIndex);
    var str2 = this.substring(startIndex + count, this.length);
    return str1 + str2;
}
 
/*
 * 移除字符串左邊指定個數的字符(如果移除的字符數超出字符串本身的長度,則不進行移除操作)
 * @param count - 要移除的字符個數
 */
String.prototype.removeLeft = function (count) {
    if (count == undefined) count = 1;
     
    if (this.length >= count) {
        return this.substring(count, this.length);
    } else {
        return this;
    }
     
}
 
/*
 * 移除字符串右邊指定個數的字符(如果移除的字符數超出字符串本身的長度,則不進行移除操作)
 * @param count - 要移除的字符個數
 */
String.prototype.removeRight = function (count) {
    if (count == undefined) count = 1;
 
    if (this.length >= count) {
        return this.substring(0, this.length-count);
    } else {
        return this;
    }
}
 
 
/*
 * 把字符串轉換爲數字(整數或小數)
 * @param defaultValue - 轉換失敗時返回的默認值
 */
String.prototype.toNumber = function (defaultValue) {
    if (isNaN(this)) {
        return defaultValue;
    } else {
        return Number(this);
    }
}
 
/*
 * 把字符串轉換爲布爾類型
 * @param defaultValue - 轉換失敗時返回的默認值
 */
String.prototype.toBool = function (defaultValue) {
    var str = this.toLowerCase();
    if (str == "true") {
        return true;
    } else if (str == "false") {
        return false;
    } else {
        return defaultValue;
    }
}
 
 
 
/*
 * 把字符串轉換爲日期類型
 * @param defaultValue - 轉換失敗時返回的默認值
 */
String.prototype.toDate = function (defaultValue) {
    var str = this.replace(/-/g, "/");
    var date = new Date(str);
    if (date == 'Invalid Date') {
        return defaultValue;
    } else {
        return date;
    }
}
 
 
 
 
 
/************************************ 數據校驗 **************************************/
 
/* 
 * 自定義正則表達式驗證 
 * @param pattern - 正則表達式 
 * @return 驗證成功返回true,失敗返回false 
 */
String.prototype.isMatch=function (pattern) {
    if (this == undefined || pattern == undefined) {
        return false;
    }
    return pattern.test(this);
}
 
 
/* 
 * 判斷字符串爲null或者爲空字符串
 * @return 驗證成功返回true,失敗返回false 
 */
String.prototype.isNull = function () {
    if (this == undefined) {
        return true;
    }
    if (this.trim() == "") {
        return true;
    }
    return false;
}
 
/* 
 * 判斷字符串不爲null並且不爲空字符串
 * @return 驗證成功返回true,失敗返回false 
 */
String.prototype.isNotNull = function () {
    return !this.isNull();
}
 
/* 
 * 字符串長度驗證 
 * @param field - 驗證的字段 
 * @param minLength - 字符串允許最小的長度 - null表示不限制字符串的最小長度 
 * @param maxLength - 字符串允許最大的長度 - null表示不限制字符串的最大長度 
 * @return 驗證成功返回true,失敗返回false 
 */
String.prototype.compareLength = function (minLength, maxLength) {
    if (minLength != null) {
        if (this.length < minLength) {
            return false;
        }
    }
    if (maxLength != null) {
        if (this.length > maxLength) {
            return false;
        }
    }
    return true;
}
 
/* 
 * 整數格式驗證 
 * @return 驗證成功返回true,失敗返回false 
 */
String.prototype.isInt = function () {
    if (this == undefined) {
        return false;
    }
    return /^((\d)|([1-9]\d+))$/.test(this);
}
 
/* 
 * 小數格式驗證 
 * @return 驗證成功返回true,失敗返回false 
 */
String.prototype.isDecimal = function () {
    if (this == undefined) {
        return false;
    }
    return /^\d+\.\d+$/.test(this);
}
 
/* 
 * 整數或小數格式驗證  
 * @return 驗證成功返回true,失敗返回false 
 */
String.prototype.isNumber = function () {
    if (this == undefined) {
        return false;
    }
    return this.isInt()||this.isDecimal();
}
 
/* 
 * 數值大小驗證 
 * @param field - 驗證的字段 
 * @param minValue - 允許的最小值 - null表示不限制最小值 
 * @param maxValue - 允許的最大值 - null表示不限制最大值 
 * @return 驗證成功返回true,失敗返回false 
 */
String.prototype.compareValue = function (minValue, maxValue) {
    var value = this.toNumber();
 
    if (minValue != null) {
        if (value < minValue) {
            return false;
        }
    }
    if (maxValue != null) {
        if (value > maxValue) {
            return false;
        }
    }
    return true;
}
 
 
/* 
 * 郵箱格式驗證 
 * @return 驗證成功返回true,失敗返回false 
 */
String.prototype.isEmail = function () {
    if (this == undefined) {
        return false;
    }
    //匹配郵箱,比如[email protected]或者[email protected] 
    return /^\w+@\w+(\.[a-zA-Z]{2,3}){1,2}$/.test(this);
}
 
/* 
 * 電話號碼格式驗證 
 * @return 驗證成功返回true,失敗返回false 
 */
String.prototype.isPhone = function () {
    if (this == undefined) {
        return false;
    }
    //電話號碼,區號3~4位,電話號碼7~8位,分機號3~4位,區號和分機號可以忽略 
    return /^(\d{3,4}-)?\d{7,8}(-\d{3,4})?$/.test(this);
}
 
/* 
 * 手機號碼格式驗證 
 * @return 驗證成功返回true,失敗返回false 
 */
String.prototype.isTelephone = function () {
    if (this == undefined) {
        return false;
    }
    //手機號碼,目前手機號碼都是從1開頭的,一個11位數字 
    return /^1\d{10}$/.test(this);
}
 
/* 
 * 中文格式驗證 (只要其中一個字符不爲中文都會返回false)
 * @return 驗證成功返回true,失敗返回false 
 */
String.prototype.isChinese = function () {
    if (this == undefined) {
        return false;
    }
    return /^[\u4e00-\u9fa5]+$/.test(this);
}
 
/* 
 * 網址Url格式驗證 
 * @return 驗證成功返回true,失敗返回false 
 */
String.prototype.isUrl = function () {
    if (this == undefined) {
        return false;
    }
    //網址可帶任意個請求參數 
    var reg = /^http:\/\/(\S+\/?)*(\w+(\?\w+=[^&=\s]+(&\w+=[^&=\s]+)*)?)?$/;
    return reg.test(this);
}
 
/* 
 * 日期格式驗證 
 * @return 驗證成功返回true,失敗返回false 
 */
String.prototype.isDate = function () {
    return this.toDate(1) != 1;
}
 
 
/* 
 * 日期大小驗證 
 * @param minDate - 日期允許的最小值 - null表示不限制日期的最小值(參數類型可以是字符串也可以是日期)
 * @param maxValue - 日期允許的最大值 - null表示不限制日期的最大值 (參數類型可以是字符串也可以是日期)
 * @return 驗證成功返回true,失敗返回false 
 */
String.prototype.compareDate = function (minValue,maxValue) {
    var dateValue = this.toDate();
    if (minValue != null) {
        if (typeof minValue == "string") {
            minValue=minValue.toDate();
        }
        if (dateValue < minValue) {
            return false;
        }
    }
    if (maxValue != null) {
        if (typeof maxValue == "string") {
            maxValue = maxValue.toDate();
        }
        if (dateValue > maxValue) {
            return false;
        }
    }
    return true;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章