posts - 104,  comments - 34,  trackbacks - 0
          在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>
          posted on 2007-12-03 23:45 末日風情 閱讀(2440) 評論(0)  編輯  收藏 所屬分類: javascript
          <2007年12月>
          2526272829301
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          常用鏈接

          留言簿(4)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 宝坻区| 邯郸市| 福州市| 福建省| 贵定县| 离岛区| 青田县| 雷山县| 平安县| 天台县| 赤城县| 玉门市| 黎平县| 庐江县| 阿拉善右旗| 来安县| 新河县| 巴塘县| 康乐县| 宁安市| 鹿邑县| 沽源县| 东海县| 堆龙德庆县| 惠安县| 尼玛县| 庆城县| 玉屏| 灵丘县| 苏尼特右旗| 新兴县| 拉萨市| 涪陵区| 甘谷县| 府谷县| 方城县| 本溪市| 永清县| 松潘县| 柘城县| 康马县|