JavaScript字符串常用操作

2014-07-01 13:53:59  访问(2277) 赞(0) 踩(0)


    // Trim 过滤
    function StringTrim(str) {
        // Trim //
        if (str == null)
            return "";

        return str.replace(/(^\s*)|(\s*$)/g, "");

    }

    // Trim 左边过滤
    function StringLeftTrim(str) {
        if (str == null)
            return "";

        return str.replace(/(\s*)(.*)/g, "$2");
    }


    // 反转 //
    function StringReverse(str) {

        if (str == null)
            return "";

        var theResult = "";
        var theLen = str.length;

        for (var i = theLen - 1; i >= 0; --i) {
            theResult = theResult + str.substr(i, 1);
        }
        return theResult;
    }

    // Trim 右边过滤 //
    function StringRightTrim(str) {

        if (str == null)
            return "";

        str = StringReverse(str);

        str = StringLeftTrim(str);

        str = StringReverse(str);

        return str;
    }
    
    
    // 是否为空 //
    function StringIsBlank(str) {
        // 是否为空 //
        if (str == null)
            return true;

        str = str + "";

        str = StringTrim(str);

        if (str == "")
            return true;

        return false;
    } 

    // 小写 //
    function StringGetLowerCase(str) {

        if (str == null)
            return "";

        return str.toLowerCase();
    }

    // 大写 //
    function StringGetUpperCase(str) {

        if (str == null)
            return "";

        return str.toUpperCase();
    } 

    // 是否开始于 //
    function StringStartsWith(str, theChar) {
        
        if (str == null || str == "" || theChar == null || theChar == "")
            return false;

        var idx = str.indexOf(theChar);

        if (idx == 0)
            return true;
        else
            return false;
    }

    function StringNotStartsWith(str, theChar) {
        // 是否不开始于 //
        if (str == null || str == "" || theChar == null || theChar == "")
            return true;

        var idx = str.indexOf(theChar);

        if (idx == 0)
            return false;
        else
            return true;
    }

    // 子记录
    function StringSubString(str, startIndex) {
        if (str == null || str == "")
            return "";

        if(startIndex == 0)
            return str;

        var theLen = str.length;

        if (startIndex >= theLen)
            return str;

        return str.substr(startIndex, theLen);
    }

示例







    
        字符串操作__慢子助手编程交流与共享网
    
    
    


    

字符串操作

输入:
输出:
        
        
     

运行代码

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)