asfman
android developer
posts - 90,  comments - 213,  trackbacks - 0

利用Javascript中每个对象(Object)的prototype属性我们可以为Javascript中的内置对象添加我们自己的方法和属性。
以下我们就用这个属性来为String对象添加三个方法:Trim,LTrim,RTrim(作用和VbScript中的同名函数一样)
String.prototype.Trim = function()
{
    return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.LTrim = function()
{
    return this.replace(/(^\s*)/g, "");
}
String.prototype.Rtrim = function()
{
    return this.replace(/(\s*$)/g, "");
}
怎么样,简单吧,下面看一个使用的实例:
<script language=javascript>
String.prototype.Trim = function()
{
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

var s = "    leading and trailing spaces    ";

window.alert(s + " (" + s.length + ")");

s = s.Trim();

window.alert(s + " (" + s.length + ")");

</script>

posted on 2006-04-03 15:07 汪杰 阅读(149) 评论(0)  编辑 收藏 引用 所属分类: hengxing网站js
只有注册用户登录后才能发表评论。

<2026年3月>
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234

常用链接

留言簿(19)

随笔分类(1)

随笔档案(90)

文章分类(727)

文章档案(712)

相册

收藏夹

http://blog.csdn.net/prodigynonsense

友情链接

最新随笔

搜索

  •  

积分与排名

  • 积分 - 503251
  • 排名 - 6

最新随笔

最新评论

阅读排行榜

评论排行榜