在Javascript中为String对象添加trim,ltrim,rtrim方法
来源:互联网 发布:淘宝服装免费代理商 编辑:程序博客网 时间:2024/05/21 18:34
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
利用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>
<<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
利用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>
<<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- 在Javascript中为String对象添加trim,ltrim,rtrim
- JavaScript中为String对象添加 trim / ltrim / rtrim 方法
- JavaScript--在Javascript中为String对象添加trim,ltrim,rtrim方法
- 在Javascript中为String对象添加trim,ltrim,rtrim方法
- 在Javascript中为String对象添加trim,ltrim,rtrim方法
- 在Javascript中为String对象添加trim,ltrim,rtrim方法
- 在Javascript中为String对象添加trim,ltrim,rtrim方法
- 在Javascript中为String对象添加trim,ltrim,rtrim方法
- 在Javascript中为String对象添加trim,ltrim,rtrim方法
- 在Javascript中为String对象添加trim,ltrim,rtrim方法
- 为JavaScript添加trim,ltrim,rtrim函数
- 在javascript中给string对象填加trim,ltrim,rtrim方向
- javascript中去除空格Trim(),LTrim(),RTrim()
- javascript中去除空格Trim(),LTrim(),RTrim()
- Javascript中为String对象添加 Trim() 方法
- SQL中LTrim(string),RTrim(string),Trim(string)的用法?
- JavaScript实现trim() 、Ltrim() 、Rtrim()
- oracle中trim,rtrim,ltrim
- 基于tcp/ip协议的Socket网络通讯 --> 简单的数据传送和库构造
- 在ASP中,用JScript脚本实现分页的另类办法。
- Java WS和二进制数据传送
- 在b/s开发中经常用到的javaScript技术
- java 调用ActiveDirectory,使用标准的LDAP协议
- 在Javascript中为String对象添加trim,ltrim,rtrim方法
- 有关LDAP协议的整理
- 在一段文字里点一下就可以在里面插入一段文字
- 使用ProcessBuilder调用外部命令,并返回大量结果
- 站内搜索脚本例子二(JavaScript)
- JSP中图片验证的实现
- 站内搜索脚本例子六(JavaScript)
- JDO学习笔记 Sun JDO参考实现篇
- 站内搜索脚本例子三(JavaScript)