码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript String.prototype 原型

时间:2016-06-21 12:23:44      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:

  1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2 <html>
  3 <head>
  4     <title>String.prototype</title>
  5     <meta name="Generator" content="EditPlus">
  6     <meta name="Author" content="">
  7     <meta name="Keywords" content="">
  8     <meta name="Description" content="">
  9     <script language="JavaScript">
 10         function replace(obj)
 11         {
 12             alert(obj.value.Replace("a","d"));
 13         }
 14         window.onload=function(){
 15             alert("123456".movePoint(-2));
 16         }
 17         </script>
 18 </head>
 19 <body>
 20     <span>String.Replace </span><br />
 21     <input type="TEXT" value="replace" onclick="replace(this)">
 22 </body>
 23 
 24 <script type=‘text/javascript‘>
 25         /**
 26          * 左补齐字符串
 27          * 
 28          * @param nSize
 29          *            要补齐的长度
 30          * @param ch
 31          *            要补齐的字符
 32          * @return
 33          */
 34         String.prototype.padLeft = function(nSize, ch)
 35         {
 36             var len = 0;
 37             var s = this ? this : "";
 38             ch = ch ? ch : 0;<!--默认补0
 39 
 40             len = s.length;
 41             while (len < nSize)
 42             {
 43                 s = ch + s;
 44                 len++;
 45             }
 46             return s;
 47         };
 48         /**
 49          * 右补齐字符串
 50          * 
 51          * @param nSize
 52          *            要补齐的长度
 53          * @param ch
 54          *            要补齐的字符
 55          * @return
 56          */
 57         String.prototype.padRight = function(nSize, ch)
 58         {
 59             var len = 0;
 60             var s = this ? this : "";
 61             ch = ch ? ch : 0;<!--默认补0
 62 
 63             len = s.length;
 64             while (len < nSize)
 65             {
 66                 s = s + ch;
 67                 len++;
 68             }
 69             return s;
 70         };
 71         /**
 72          * 左移小数点位置(用于数学计算,相当于除以Math.pow(10,scale))
 73          * 
 74          * @param scale
 75          *            要移位的刻度
 76          * @return
 77          */
 78         String.prototype.movePointLeft = function(scale)
 79         {
 80             var s, s1, s2, ch, ps, sign;
 81             ch = .;
 82             sign = ‘‘;
 83             s = this ? this : "";
 84 
 85             if (scale <= 0) return s;
 86             ps = s.split(.);
 87             s1 = ps[0] ? ps[0] : "";
 88             s2 = ps[1] ? ps[1] : "";
 89             if (s1.slice(0, 1) == -)
 90             {
 91                 s1 = s1.slice(1);
 92                 sign = -;
 93             }
 94             if (s1.length <= scale)
 95             {
 96                 ch = "0.";
 97                 s1 = s1.padLeft(scale);
 98             }
 99             return sign + s1.slice(0, -scale) + ch + s1.slice(-scale) + s2;
100         };
101         /**
102          * 右移小数点位置(用于数学计算,相当于乘以Math.pow(10,scale))
103          * 
104          * @param scale
105          *            要移位的刻度
106          * @return
107          */
108         String.prototype.movePointRight = function(scale)
109         {
110             var s, s1, s2, ch, ps;
111             ch = .;
112             s = this ? this : "";
113 
114             if (scale <= 0) return s;
115             ps = s.split(.);
116             s1 = ps[0] ? ps[0] : "";
117             s2 = ps[1] ? ps[1] : "";
118             if (s2.length <= scale)
119             {
120                 ch = ‘‘;
121                 s2 = s2.padRight(scale);
122             }
123             return s1 + s2.slice(0, scale) + ch + s2.slice(scale, s2.length);
124         };
125         /**
126          * 移动小数点位置(用于数学计算,相当于(乘以/除以)Math.pow(10,scale))
127          * 
128          * @param scale
129          *            要移位的刻度(正数表示向右移;负数表示向左移动;0返回原值)
130          * @return
131          */
132         String.prototype.movePoint = function(scale)
133         {
134             if (scale >= 0)
135                 return this.movePointRight(scale);
136             else
137                 return this.movePointLeft(-scale);
138         };
139         <!--字符串替换-->
140         String.prototype.Replace = function(oldValue,newValue) 
141         {
142             var reg = new RegExp(oldValue,"g"); 
143             return this.replace(reg, newValue); 
144         }
145         <!--判断字符串是否以指定的字符串结束-->
146         String.prototype.EndsWith = function(str) 
147         {
148             return this.substr(this.length - str.length) == str;
149         }
150         <!--去掉字符左端的的空白字符-->
151         String.prototype.LeftTrim = function()
152         {
153             return this.replace(/(^[//s]*)/g, "");
154         }
155         <!--去掉字符右端的空白字符-->
156         String.prototype.RightTrim = function()
157         {
158             return this.replace(/([//s]*$)/g, "");
159         }
160         <!--判断字符串是否以指定的字符串开始-->
161         String.prototype.StartsWith = function(str) 
162         {
163             return this.substr(0, str.length) == str;
164         }
165 
166 </script>
167 </html>

 

可以把<script type=‘text/javascript‘> </script>之间的内容直接放到JS文件中,就可以调用String的方法。对于var的变量可以先toString()在调用。

比如:

item23 = document.getElementById(documentArray[0]+"_23").value;

Number(item23.toString().movePoint(-2))

JavaScript String.prototype 原型

标签:

原文地址:http://www.cnblogs.com/hehongtao/p/5603093.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!