js 字符串操作函數

concat() – 將兩個或多個字符的文本組合起來,返回一個新的字符串。
indexOf() – 返回字符串中一個子串第一處出現的索引。如果沒有匹配項,返回 -1 。
charAt() – 返回指定位置的字符。
lastIndexOf() – 返回字符串中一個子串最後一處出現的索引,如果沒有匹配項,返回 -1 。
match() – 檢查一個字符串是否匹配一個正則表達式。
substring() – 返回字符串的一個子串。傳入參數是起始位置和結束位置。
replace() – 用來查找匹配一個正則表達式的字符串,然後使用新字符串代替匹配的字符串。
search() – 執行一個正則表達式匹配查找。如果查找成功,返回字符串中匹配的索引值。否則返回 -1 。
slice() – 提取字符串的一部分,並返回一個新字符串。
split() – 通過將字符串劃分成子串,將一個字符串做成一個字符串數組。
length – 返回字符串的長度,所謂字符串的長度是指其包含的字符的個數。
toLowerCase() – 將整個字符串轉成小寫字母。
toUpperCase() – 將整個字符串轉成大寫字母。

 

字符串函數擴充   http://www.cnblogs.com/skycode/archive/2008/07/22/1712028.html

===========================================

//去除左邊的空格

===========================================

*/

String.prototype.LTrim = function()

{

        return this.replace(/(^\s*)/g, "");

}

/*

===========================================

//去除右邊的空格

===========================================

*/

String.prototype.Rtrim = function()

{

        return this.replace(/(\s*$)/g, "");

}

/*

===========================================

//去除前後空格

===========================================

*/

String.prototype.Trim = function()

{

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

}

/*

===========================================

//得到左邊的字符串

===========================================

*/

String.prototype.Left = function(len)

{

        if(isNaN(len)||len==null)

        {

                len = this.length;

        }

        else

        {

                if(parseInt(len)<0||parseInt(len)>this.length)

                {

                        len = this.length;

                }

        }

       

        return this.substr(0,len);

}

/*

===========================================

//得到右邊的字符串

===========================================

*/

String.prototype.Right = function(len)

{

        if(isNaN(len)||len==null)

        {

                len = this.length;

        }

        else

        {

                if(parseInt(len)<0||parseInt(len)>this.length)

                {

                        len = this.length;

                }

        }

       

        return this.substring(this.length-len,this.length);

}

/*

===========================================

//得到中間的字符串,注意從0開始

===========================================

*/

String.prototype.Mid = function(start,len)

{

        return this.substr(start,len);

}

/*

===========================================

//在字符串裏查找另一字符串:位置從0開始

===========================================

*/

String.prototype.InStr = function(str)

{

        if(str==null)

        {

                str = "";

        }

       

        return this.indexOf(str);

}

/*

===========================================

//在字符串裏反向查找另一字符串:位置0開始

===========================================

*/

String.prototype.InStrRev = function(str)

{

        if(str==null)

        {

                str = "";

        }

       

        return this.lastIndexOf(str);

}

/*

===========================================

//計算字符串打印長度

===========================================

*/

String.prototype.LengthW = function()

{

        return this.replace(/[^\x00-\xff]/g,"**").length;

}

/*

===========================================

//是否是正確的IP地址

===========================================

*/

String.prototype.isIP = function()

{

        var reSpaceCheck = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;

        if (reSpaceCheck.test(this))

        {

                this.match(reSpaceCheck);

                if (RegExp.$1 <= 255 && RegExp.$1 >= 0

                 && RegExp.$2 <= 255 && RegExp.$2 >= 0

                 && RegExp.$3 <= 255 && RegExp.$3 >= 0

                 && RegExp.$4 <= 255 && RegExp.$4 >= 0)

                {

                        return true;    

                }

                else

                {

                        return false;

                }

        }

        else

        {

                return false;

        }

  

}

/*

===========================================

//是否是正確的長日期

===========================================

*/

String.prototype.isLongDate = function()

{

        var r = this.replace(/(^\s*)|(\s*$)/g, "").match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/);

        if(r==null)

        {

                return false;

        }

        var d = new Date(r[1], r[3]-1,r[4],r[5],r[6],r[7]);

        return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]&&d.getHours()==r[5]&&d.getMinutes()==r[6]&&d.getSeconds()==r[7]);

}

/*

===========================================

//是否是正確的短日期

===========================================

*/

String.prototype.isShortDate = function()

{

        var r = this.replace(/(^\s*)|(\s*$)/g, "").match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);

        if(r==null)

        {

                return false;

        }

        var d = new Date(r[1], r[3]-1, r[4]);

        return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]);

}

/*

===========================================

//是否是正確的日期

===========================================

*/

String.prototype.isDate = function()

{

        return this.isLongDate()||this.isShortDate();

}

/*

===========================================

//是否是手機

===========================================

*/

String.prototype.isMobile = function()

{

        return /^0{0,1}13[0-9]{9}$/.test(this);

}

/*

===========================================

//是否是郵件

===========================================

*/

String.prototype.isEmail = function()

{

        return /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(this);

}

/*

===========================================

//是否是郵編(中國)

===========================================

*/

String.prototype.isZipCode = function()

{

        return /^[\\d]{6}$/.test(this);

}

/*

===========================================

發佈了19 篇原創文章 · 獲贊 2 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章