String 的扩展方法

来源:无忧脚本 时间:2008-05-12 22:36:00 

//获取字符数组

String.prototype.ToCharArray=function()
{
         return this.split("");
}


//获取N个相同的字符串

String.prototype.Repeat=function(num)
{
    var tmpArr=[];
    for(var i=0;i<num;i++)    tmpArr.push(this);
    return tmpArr.join("");
}


//逆序

String.prototype.Reverse=function()
{
     return this.split("").reverse().join("");
}


//测试是否是数字

String.prototype.IsNumeric=function()
{
    var tmpFloat=parseFloat(this);
    if(isNaN(tmpFloat))    return false;
    var tmpLen=this.length-tmpFloat.toString().length;
    return tmpFloat+"0".Repeat(tmpLen)==this;
}


//测试是否是整数

String.prototype.IsInt=function()
{
    if(this=="NaN")    return false;
    return this==parseInt(this).toString();
}


// 合并多个空白为一个空白

String.prototype.resetBlank = function()
{
    return this.replace(/s+/g," ");
}


// 除去左边空白

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.getNum = function()
{
    return this.replace(/[^d]/g,"");
}


// 保留字母

String.prototype.getEn = function()
{
    return this.replace(/[^A-Za-z]/g,""); 
}


// 保留中文

String.prototype.getCn = function()
{
    return this.replace(/[^u4e00-u9fa5uf900-ufa2d]/g,"");
}


// 得到字节长度

String.prototype.getRealLength = function()
{
    return this.replace(/[^x00-xff]/g,"--").length;
}

// 从左截取指定长度的字串

String.prototype.left = function(n)
{
    return this.slice(0,n);
}


// 从右截取指定长度的字串

String.prototype.right = function(n)
{
    return this.slice(this.length-n);
}

// HTML编码

String.prototype.HTMLEncode = function()
{
    var re = this;
    var q1 = [/x26/g,/x3C/g,/x3E/g,/x20/g];
    var q2 = ["&","<",">"," "];
    for(var i=0;i<q1.length;i++)
    re = re.replace(q1[i],q2[i]);
    return re;
}

// Unicode转化

String.prototype.ascW = function()
{
    var strText = "";
    for (var i=0; i<this.length; i++) strText += "&#" + this.charCodeAt(i) + ";";
    return strText;
标签:string,扩展
0
投稿

猜你喜欢

  • 详解Vue.js 可拖放文本框组件的使用

    2024-04-27 15:47:22
  • oracle 查询表名以及表的列名

    2009-07-26 09:33:00
  • JS中的THIS和WINDOW.EVENT.SRCELEMENT详解

    2023-07-20 20:48:51
  • 9种使用Chrome Firefox 自带调试工具调试javascript技巧

    2023-07-19 01:03:48
  • python dlib人脸识别代码实例

    2021-04-05 12:57:33
  • CSS的学习应该注意学习方法

    2007-11-27 00:20:00
  • 解决pip install报错:Cannot connect to proxy问题

    2021-05-01 01:14:11
  • 如何用 Python 处理不平衡数据集

    2023-02-21 07:35:31
  • Python中optparser库用法实例详解

    2023-08-14 01:32:11
  • 利用打码兔和超人打码自封装的打码类分享

    2023-03-13 21:44:40
  • 一个导航的前端实现

    2008-11-13 12:41:00
  • Python将xml和xsl转换为html的方法

    2022-04-02 14:24:32
  • selenium+opencv实现滑块验证码的登陆

    2022-03-28 06:49:04
  • Python的包管理器pip更换软件源的方法详解

    2023-02-03 05:25:22
  • Python探索之实现一个简单的HTTP服务器

    2021-06-04 16:52:02
  • Python绘图模块 turtle案例代码

    2022-12-16 01:28:10
  • 分析uniapp如何动态获取接口域名

    2024-04-10 13:59:26
  • python3.6.3+opencv3.3.0实现动态人脸捕获

    2022-12-21 11:59:41
  • Python 实现数据结构-堆栈和队列的操作方法

    2021-05-25 20:56:16
  • python中正则表达式findall的用法实例

    2022-02-24 07:51:28
  • asp之家 网络编程 m.aspxhome.com