zoukankan      html  css  js  c++  java
  • 字符串工具类

    /**
     * @author: 苗士军
     * @description 字符串工具类
     */
    StringUtils = {
        /**
         * @description 判断字符串是否为空
         * @param input
         * @return {boolean}
         */
        isEmpty: function(input) {
            return input == null || input == '';
        },
        /**
         * @description 判断字符串是否不为空
         * @param input
         * @return {boolean}
         */
        isNotEmpty: function(input) {
            return !this.isEmpty(input);
        },
        /**
         * @description 判断字符串是否全是空格
         * @param input
         * @return {boolean}
         */
        isBlank: function(input) {
            return input == null || /^s*$/.test(input);
        },
        /**
         * @description 判断字符串是否不全是空格
         * @param input
         * @return {boolean}
         */
        isNotBlank: function(input) {
            return !this.isBlank(input);
        },
        /**
         * @description 去除字符串首尾空格
         * @param input
         * @return {void|string|XML}
         */
        trim: function(input) {
            return input.replace(/^s+|s+$/g, '');
        },
        /**
         * @description 输入去掉首尾空格
         * @param input
         * @return {*}
         */
        trimToEmpty: function(input) {
            return input == null ? "" : this.trim(input);
        },
        /**
         * @description 字符串是否以字符串开头
         * @param input
         * @param prefix
         * @return {boolean}
         */
        startsWith: function(input, prefix) {
            return input.indexOf(prefix) === 0;
        },
        /**
         * @description 字符串是否以字符串结尾
         * @param input
         * @param prefix
         * @return {boolean}
         */
        endsWith: function(input, suffix) {
            return input.lastIndexOf(suffix) === 0;
        },
        /**
         * @description 字符串是否包含某些字符串
         * @param input
         * @param searchSeq
         * @return {boolean}
         */
        contains: function(input, searchSeq) {
            return input.indexOf(searchSeq) >= 0;
        },
        /**
         * @description 判断字符串是否相等
         * @param input1
         * @param input2
         * @return {boolean}
         */
        equals: function(input1, input2) {
            return input1 == input2;
        },
        /**
         * @description 字符串忽略大小写比较是否相等
         * @param input1
         * @param input2
         * @return {boolean}
         */
        equalsIgnoreCase: function(input1, input2) {
            return input1.toLocaleLowerCase() == input2.toLocaleLowerCase();
        },
        /**
         * @description 判断字符串是否包含空格
         * @param input
         * @return {*|boolean}
         */
        containsWhitespace: function(input) {
            return this.contains(input, ' ');
        },
        /**
         * @description 生成指定个数的字符
         * @param ch
         * @param repeatTimes
         * @return {string}
         */
        repeat: function(ch, repeatTimes) {
            var result = "";
            for(var i = 0; i < repeatTimes; i++) {
                result += ch;
            }
            return result;
        },
        /**
         * @description 去除全部空格
         * @param input
         * @return {void|string|XML}
         */
        deleteWhitespace: function(input) {
            return input.replace(/s+/g, '');
        },
        /**
         * @description 字符串右边添加重复几次的固定字符串
         * @param input
         * @param size
         * @param padStr
         * @return {*}
         */
        rightPad: function(input, size, padStr) {
            return input + this.repeat(padStr, size);
        },
        /**
         * @description 字符串左边添加重复几次的固定字符串
         * @param input
         * @param size
         * @param padStr
         * @return {*}
         */
        leftPad: function(input, size, padStr) {
            return this.repeat(padStr, size) + input;
        },
        /**
         * @description  首小写字母转大写
         * @param input
         * @return {*}
         */
        capitalize: function(input) {
            var strLen = 0;
            if(input == null || (strLen = input.length) == 0) {
                return input;
            }
            return input.replace(/^[a-z]/, function(matchStr) {
                return matchStr.toLocaleUpperCase();
            });
        },
        /**
         * @description 首大写字母转小写
         * @param input
         * @return {*}
         */
        uncapitalize: function(input) {
            var strLen = 0;
            if(input == null || (strLen = input.length) == 0) {
                return input;
            }
            return input.replace(/^[A-Z]/, function(matchStr) {
                return matchStr.toLocaleLowerCase();
            });
        },
        /**
         * @description 大写转小写,小写转大写
         * @param input
         * @return {void|string|XML}
         */
        swapCase: function(input) {
            return input.replace(/[a-z]/ig, function(matchStr) {
                if(matchStr >= 'A' && matchStr <= 'Z') {
                    return matchStr.toLocaleLowerCase();
                } else if(matchStr >= 'a' && matchStr <= 'z') {
                    return matchStr.toLocaleUpperCase();
                }
            });
        },
        /**
         * @description 统计含有的子字符串的个数
         * @param input
         * @param sub
         * @return {number}
         */
        countMatches: function(input, sub) {
            if(this.isEmpty(input) || this.isEmpty(sub)) {
                return 0;
            }
            var count = 0;
            var index = 0;
            while((index = input.indexOf(sub, index)) != -1) {
                index += sub.length;
                count++;
            }
            return count;
        },
        /**
         * @description 只包含字母
         * @param input
         * @return {boolean}
         */
        isAlpha: function(input) {
            return /^[a-z]+$/i.test(input);
        },
        /**
         * @description 只包含字母、空格
         * @param input
         * @return {boolean}
         */
        isAlphaSpace: function(input) {
            return /^[a-zs]*$/i.test(input);
        },
        /**
         * @description 只包含字母、数字
         * @param input
         * @return {boolean}
         */
        isAlphanumeric: function(input) {
            return /^[a-z0-9]+$/i.test(input);
        },
        /**
         * @description 只包含字母、数字和空格
         * @param input
         * @return {boolean}
         */
        isAlphanumericSpace: function(input) {
            return /^[a-z0-9s]*$/i.test(input);
        },
        /**
         * @description 数字
         * @param input
         * @return {boolean}
         */
        isNumeric: function(input) {
            return /^(?:[1-9]d*|0)(?:.d+)?$/.test(input);
        },
        /**
         * @description 小数
         * @param input
         * @return {boolean}
         */
        isDecimal: function(input) {
            return /^[-+]?(?:0|[1-9]d*).d+$/.test(input);
        },
        /**
         * @description 小数(只能出现-)
         * @param input
         * @return {boolean}
         */
        isNegativeDecimal: function(input) {
            return /^-?(?:0|[1-9]d*).d+$/.test(input);
        },
        /**
         * @description 小数(只能出现+)
         * @param input
         * @return {boolean}
         */
        isPositiveDecimal: function(input) {
            return /^+?(?:0|[1-9]d*).d+$/.test(input);
        },
        /**
         * @description 整数
         * @param input
         * @return {boolean}
         */
        isInteger: function(input) {
            return /^[-+]?(?:0|[1-9]d*)$/.test(input);
        },
        /**
         * @description 整数(+)
         * @param input
         * @return {boolean}
         */
        isPositiveInteger: function(input) {
            return /^+?(?:0|[1-9]d*)$/.test(input);
        },
        /**
         * @description 整数(-)
         * @param input
         * @return {boolean}
         */
        isNegativeInteger: function(input) {
            return /^-?(?:0|[1-9]d*)$/.test(input);
        },
        /**
         * @description 只包含数字和空格
         * @param input
         * @return {boolean}
         */
        isNumericSpace: function(input) {
            return /^[ds]*$/.test(input);
        },
        /**
         * @description 碰到字符串是否是空格
         * @param input
         * @return {boolean}
         */
        isWhitespace: function(input) {
            return /^s*$/.test(input);
        },
        /**
         * @description 判断字符串是否全是小写
         * @param input
         * @return {boolean}
         */
        isAllLowerCase: function(input) {
            return /^[a-z]+$/.test(input);
        },
        /**
         * @description 判断字符串是否全是大写
         * @param input
         * @return {boolean}
         */
        isAllUpperCase: function(input) {
            return /^[A-Z]+$/.test(input);
        },
        /**
         * @description 字符串初始化
         * @param input
         * @param defaultStr
         * @return {*}
         */
        defaultString: function(input, defaultStr) {
            return input == null ? defaultStr : input;
        },
        /**
         * @description 字符串初始化
         * @param input
         * @param defaultStr
         * @return {*}
         */
        defaultIfBlank: function(input, defaultStr) {
            return this.isBlank(input) ? defaultStr : input;
        },
        /**
         * @description 字符串初始化
         * @param input
         * @param defaultStr
         * @return {*}
         */
        defaultIfEmpty: function(input, defaultStr) {
            return this.isEmpty(input) ? defaultStr : input;
        },
        /**
         * @description 字符串反转
         * @param input
         * @return {string}
         */
        reverse: function(input) {
            if(this.isBlank(input)) {
                input;
            }
            return input.split("").reverse().join("");
        },
        /**
         * @description 删掉特殊字符(英文状态下)
         * @param input
         * @return {string|XML|void}
         */
        removeSpecialCharacter: function(input) {
            return input.replace(/[!-/:-@[-`{-~]/g, "");
        },
        /**
         * @description 只包含特殊字符、数字和字母(不包括空格,若想包括空格,改为[ -~])
         * @param input
         * @return {boolean}
         */
        isSpecialCharacterAlphanumeric: function(input) {
            return /^[!-~]+$/.test(input);
        },
        /**
         * @description 校验时排除某些字符串,即不能包含某些字符串
         * @param input
         * @param conditions {Object} conditions:里面有多个属性,如下:
         *  @param {String} matcherFlag 匹配标识
         * 0:数字;1:字母;2:小写字母;3:大写字母;4:特殊字符,指英文状态下的标点符号及括号等;5:中文;
         * 6:数字和字母;7:数字和小写字母;8:数字和大写字母;9:数字、字母和特殊字符;10:数字和中文;
         * 11:小写字母和特殊字符;12:大写字母和特殊字符;13:字母和特殊字符;14:小写字母和中文;15:大写字母和中文;
         * 16:字母和中文;17:特殊字符、和中文;18:特殊字符、字母和中文;19:特殊字符、小写字母和中文;20:特殊字符、大写字母和中文;
         * 100:所有字符;
         * @param {Array} excludeStrArr 排除的字符串,数组格式
         * @param {String} length 长度,可为空。1,2表示长度1到2之间;10,表示10个以上字符;5表示长度为5
         * @param {Boolean} ignoreCase 是否忽略大小写
         * conditions={matcherFlag:"0",excludeStrArr:[],length:"",ignoreCase:true}
         * @return {boolean}
         */
        isPatternMustExcludeSomeStr: function(input, conditions) {
            //参数
            var matcherFlag = conditions.matcherFlag;
            var excludeStrArr = conditions.excludeStrArr;
            var length = conditions.length;
            var ignoreCase = conditions.ignoreCase;
            //拼正则
            var size = excludeStrArr.length;
            var regex = (size == 0) ? "^" : "^(?!.*(?:{0}))";
            var subPattern = "";
            for(var i = 0; i < size; i++) {
                excludeStrArr[i] = StringUtils.escapeMetacharacterOfStr(excludeStrArr[i]);
                subPattern += excludeStrArr[i];
                if(i != size - 1) {
                    subPattern += "|";
                }
            }
            regex = this.format(regex, [subPattern]);
            switch(matcherFlag) {
                case '0':
                    regex += "\d";
                    break;
                case '1':
                    regex += "[a-zA-Z]";
                    break;
                case '2':
                    regex += "[a-z]";
                    break;
                case '3':
                    regex += "[A-Z]";
                    break;
                case '4':
                    regex += "[!-/:-@[-`{-~]";
                    break;
                case '5':
                    regex += "[u4E00-u9FA5]";
                    break;
                case '6':
                    regex += "[a-zA-Z0-9]";
                    break;
                case '7':
                    regex += "[a-z0-9]";
                    break;
                case '8':
                    regex += "[A-Z0-9]";
                    break;
                case '9':
                    regex += "[!-~]";
                    break;
                case '10':
                    regex += "[0-9u4E00-u9FA5]";
                    break;
                case '11':
                    regex += "[a-z!-/:-@[-`{-~]";
                    break;
                case '12':
                    regex += "[A-Z!-/:-@[-`{-~]";
                    break;
                case '13':
                    regex += "[a-zA-Z!-/:-@[-`{-~]";
                    break;
                case '14':
                    regex += "[a-zu4E00-u9FA5]";
                    break;
                case '15':
                    regex += "[A-Zu4E00-u9FA5]";
                    break;
                case '16':
                    regex += "[a-zA-Zu4E00-u9FA5]";
                    break;
                case '17':
                    regex += "[u4E00-u9FA5!-/:-@[-`{-~]";
                    break;
                case '18':
                    regex += "[u4E00-u9FA5!-~]";
                    break;
                case '19':
                    regex += "[a-zu4E00-u9FA5!-/:-@[-`{-~]";
                    break;
                case '20':
                    regex += "[A-Zu4E00-u9FA5!-/:-@[-`{-~]";
                    break;
                case '100':
                    regex += "[sS]";
                    break;
                default:
                    alert(matcherFlag + ":This type is not supported!");
            }
            regex += this.isNotBlank(length) ? "{" + length + "}" : "+";
            regex += "$";
            var pattern = new RegExp(regex, ignoreCase ? "i" : "");
            return pattern.test(input);
        },
        /**
         * @description 消息格式化
         * @param {String} message
         * @param {Array} arr
         */
        format: function(message, arr) {
            return message.replace(/{(d+)}/g, function(matchStr, group1) {
                return arr[group1];
            });
        },
        /**
         * @description 把连续出现多次的字母字符串进行压缩。如输入:aaabbbbcccccd 输出:3a4b5cd
         * @param {String} input
         * @param {Boolean} ignoreCase : true or false
         */
        compressRepeatedStr: function(input, ignoreCase) {
            var pattern = new RegExp("([a-z])\1+", ignoreCase ? "ig" : "g");
            return result = input.replace(pattern, function(matchStr, group1) {
                return matchStr.length + group1;
            });
        },
        /**
         * @description 校验必须同时包含某些字符串
         * @param {String} input
         * @param {Object} conditions:里面有多个属性,如下:
         * @param {String} matcherFlag 匹配标识
         * 0:数字;1:字母;2:小写字母;3:大写字母;4:特殊字符,指英文状态下的标点符号及括号等;5:中文;
         * 6:数字和字母;7:数字和小写字母;8:数字和大写字母;9:数字、字母和特殊字符;10:数字和中文;
         * 11:小写字母和特殊字符;12:大写字母和特殊字符;13:字母和特殊字符;14:小写字母和中文;15:大写字母和中文;
         * 16:字母和中文;17:特殊字符、和中文;18:特殊字符、字母和中文;19:特殊字符、小写字母和中文;20:特殊字符、大写字母和中文;
         * 100:所有字符;
         * @param {Array} excludeStrArr 排除的字符串,数组格式
         * @param {String} length 长度,可为空。1,2表示长度1到2之间;10,表示10个以上字符;5表示长度为5
         * @param {Boolean} ignoreCase 是否忽略大小写
         * conditions={matcherFlag:"0",containStrArr:[],length:"",ignoreCase:true}
         * @return {boolean}
         */
        isPatternMustContainSomeStr: function(input, conditions) {
            //参数
            var matcherFlag = conditions.matcherFlag;
            var containStrArr = conditions.containStrArr;
            var length = conditions.length;
            var ignoreCase = conditions.ignoreCase;
            //创建正则
            var size = containStrArr.length;
            var regex = "^";
            var subPattern = "";
            for(var i = 0; i < size; i++) {
                containStrArr[i] = Bee.StringUtils.escapeMetacharacterOfStr(containStrArr[i]);
                subPattern += "(?=.*" + containStrArr[i] + ")";
            }
            regex += subPattern;
            switch(matcherFlag) {
                case '0':
                    regex += "\d";
                    break;
                case '1':
                    regex += "[a-zA-Z]";
                    break;
                case '2':
                    regex += "[a-z]";
                    break;
                case '3':
                    regex += "[A-Z]";
                    break;
                case '4':
                    regex += "[!-/:-@[-`{-~]";
                    break;
                case '5':
                    regex += "[u4E00-u9FA5]";
                    break;
                case '6':
                    regex += "[a-zA-Z0-9]";
                    break;
                case '7':
                    regex += "[a-z0-9]";
                    break;
                case '8':
                    regex += "[A-Z0-9]";
                    break;
                case '9':
                    regex += "[!-~]";
                    break;
                case '10':
                    regex += "[0-9u4E00-u9FA5]";
                    break;
                case '11':
                    regex += "[a-z!-/:-@[-`{-~]";
                    break;
                case '12':
                    regex += "[A-Z!-/:-@[-`{-~]";
                    break;
                case '13':
                    regex += "[a-zA-Z!-/:-@[-`{-~]";
                    break;
                case '14':
                    regex += "[a-zu4E00-u9FA5]";
                    break;
                case '15':
                    regex += "[A-Zu4E00-u9FA5]";
                    break;
                case '16':
                    regex += "[a-zA-Zu4E00-u9FA5]";
                    break;
                case '17':
                    regex += "[u4E00-u9FA5!-/:-@[-`{-~]";
                    break;
                case '18':
                    regex += "[u4E00-u9FA5!-~]";
                    break;
                case '19':
                    regex += "[a-zu4E00-u9FA5!-/:-@[-`{-~]";
                    break;
                case '20':
                    regex += "[A-Zu4E00-u9FA5!-/:-@[-`{-~]";
                    break;
                case '100':
                    regex += "[sS]";
                    break;
                default:
                    alert(matcherFlag + ":This type is not supported!");
            }
            regex += this.isNotBlank(length) ? "{" + length + "}" : "+";
            regex += "$";
            var pattern = new RegExp(regex, ignoreCase ? "i" : "");
            return pattern.test(input);
        },
        /**
         * @description 中文校验
         * @param input
         * @return {boolean}
         */
        isChinese: function(input) {
            return /^[u4E00-u9FA5]+$/.test(input);
        },
        /**
         * @description 去掉中文字符
         * @param input
         * @return {string|XML|void}
         */
        removeChinese: function(input) {
            return input.replace(/[u4E00-u9FA5]+/gm, "");
        },
        /**
         * @description 转义元字符
         * @param input
         * @return {*}
         */
        escapeMetacharacter: function(input) {
            var metacharacter = "^$()*+.[]|\-?{}|";
            if(metacharacter.indexOf(input) >= 0) {
                input = "\" + input;
            }
            return input;
        },
        /**
         * @description 转义字符串中的元字符
         * @param input
         * @return {*}
         */
        escapeMetacharacterOfStr: function(input) {
            return input.replace(/[^$*+.|\-?{}|]/gm, "\$&");
        },
        /**
         * @description 增强字符串替换函数
         * @param input
         * @param oldStr
         * @param newStr
         * @return {string|XML|void}
         */
        replaceAll: function(input,oldStr,newStr)
        {
            return  input.replace(new RegExp(oldStr,"gm"),newStr);
        }
    };
  • 相关阅读:
    Dairy Queen
    求逆序对
    Factorial
    ROSETTA使用技巧随笔--PyMOL实时观测ROSETTA模拟过程中的结构变化
    ROSETTA使用技巧随笔--控制Log输出等级
    ROSETTA使用技巧随笔--Full Atom Representation和Centroid Representation
    ROSETTA使用技巧随笔--score.sc处理
    Mysql 数据库导入及导出
    管理员结束某一用户的所有进程
    bowtie2 Linux安装
  • 原文地址:https://www.cnblogs.com/miaosj/p/10677441.html
Copyright © 2011-2022 走看看