zoukankan      html  css  js  c++  java
  • JavaScript在Javascript中为String对象添加trim,ltrim,rtrim方法

    JavaScript--在Javascript中为String对象添加trim,ltrim,rtrim方法
     
    利用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>

  • 相关阅读:
    Log4Net使用
    4月博文
    论坛题目练习
    职场冷笑话两则
    初识管理的一些心得
    Project中分清楚挣值项
    预留规划项
    小感触
    好事多磨,好事成双
    忧郁
  • 原文地址:https://www.cnblogs.com/xiaodi/p/126817.html
Copyright © 2011-2022 走看看