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;
}


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