您的位置:首页 > Web前端 > JavaScript

JS一些常用的处理字符串的方法

2009-01-20 09:58 686 查看
Code

// 返回字符的长度,一个中文算2个

String.prototype.ChineseLength=function()

{

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

}

// 判断字符串是否以指定的字符串结束

String.prototype.EndsWith = function(str)

{

return this.substr(this.length - str.length) == str;

}

// 去掉字符左端的的空白字符

String.prototype.LeftTrim = function()

{

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

}

// 去掉字符右端的空白字符

String.prototype.RightTrim = function()

{

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

}

// 判断字符串是否以指定的字符串开始

String.prototype.StartsWith = function(str)

{

return this.substr(0, str.length) == str;

}

// 去掉字符两端的空白字符

String.prototype.Trim = function()

{

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

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: