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

JS 用 prototype 扩展实现string过滤空格

2013-01-22 20:35 393 查看
JS的prototype是。。。。。。

实现对字符串 头和尾 空格的过滤,代码如下所示:

//Trim the head and tail spaces
String.prototype.Trim = function () {
return this.replace(/(^\s*)|(\s*$)/g, "");
}


实现对字符串 头(左侧Left) 空格的过滤, 代码如下所示:

//Trim the head spaces of current string
String.prototype.LTrim = function () {
return this.replace(/(^\s*)/g, "");
}


实现对字符串 尾(右侧Right) 空格的过滤, 代码如下所示:

//Trim the tail spaces of current string
String.prototype.RTrim = function () {
return this.replace(/(\s*$)/g, "");
}


实现Contains方法(核心是用Index方法的返回值进行判断),代码如下所示:

//Judge current string contains substring or not
String.prototype.Contains = function (subStr) {
var currentIndex = this.indexOf(subStr);
if (currentIndex != -1) {
return true;
}
else {
return false;
}
}


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