<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>String.prototype</title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<script language="JavaScript">
function replace(obj)
{
alert(obj.value.Replace("a","d"));
}
window.onload=function(){
alert("123456".movePoint(-2));
}
</script>
</head>
<body>
<span>String.Replace </span><br />
<input type="TEXT" value="replace" onclick="replace(this)">
</body> <script type='text/javascript'>
/**
* 左补齐字符串
*
* @param nSize
* 要补齐的长度
* @param ch
* 要补齐的字符
* @return
*/
String.prototype.padLeft = function(nSize, ch)
{
var len = 0;
var s = this ? this : "";
ch = ch ? ch : '0';<!--默认补0 len = s.length;
while (len < nSize)
{
s = ch + s;
len++;
}
return s;
};
/**
* 右补齐字符串
*
* @param nSize
* 要补齐的长度
* @param ch
* 要补齐的字符
* @return
*/
String.prototype.padRight = function(nSize, ch)
{
var len = 0;
var s = this ? this : "";
ch = ch ? ch : '0';<!--默认补0 len = s.length;
while (len < nSize)
{
s = s + ch;
len++;
}
return s;
};
/**
* 左移小数点位置(用于数学计算,相当于除以Math.pow(10,scale))
*
* @param scale
* 要移位的刻度
* @return
*/
String.prototype.movePointLeft = function(scale)
{
var s, s1, s2, ch, ps, sign;
ch = '.';
sign = '';
s = this ? this : ""; if (scale <= 0) return s;
ps = s.split('.');
s1 = ps[0] ? ps[0] : "";
s2 = ps[1] ? ps[1] : "";
if (s1.slice(0, 1) == '-')
{
s1 = s1.slice(1);
sign = '-';
}
if (s1.length <= scale)
{
ch = "0.";
s1 = s1.padLeft(scale);
}
return sign + s1.slice(0, -scale) + ch + s1.slice(-scale) + s2;
};
/**
* 右移小数点位置(用于数学计算,相当于乘以Math.pow(10,scale))
*
* @param scale
* 要移位的刻度
* @return
*/
String.prototype.movePointRight = function(scale)
{
var s, s1, s2, ch, ps;
ch = '.';
s = this ? this : ""; if (scale <= 0) return s;
ps = s.split('.');
s1 = ps[0] ? ps[0] : "";
s2 = ps[1] ? ps[1] : "";
if (s2.length <= scale)
{
ch = '';
s2 = s2.padRight(scale);
}
return s1 + s2.slice(0, scale) + ch + s2.slice(scale, s2.length);
};
/**
* 移动小数点位置(用于数学计算,相当于(乘以/除以)Math.pow(10,scale))
*
* @param scale
* 要移位的刻度(正数表示向右移;负数表示向左移动;0返回原值)
* @return
*/
String.prototype.movePoint = function(scale)
{
if (scale >= 0)
return this.movePointRight(scale);
else
return this.movePointLeft(-scale);
};
<!--字符串替换-->
String.prototype.Replace = function(oldValue,newValue)
{
var reg = new RegExp(oldValue,"g");
return this.replace(reg, newValue);
}
<!--判断字符串是否以指定的字符串结束-->
String.prototype.EndsWith = function(str)
{
return this.substr(this.length - str.length) == str;
}
<!--去掉字符左端的的空白字符-->
String.prototype.LeftTrim = function()
{
return this.replace(/(^[//s]*)/g, "");
}
<!--去掉字符右端的空白字符-->
String.prototype.RightTrim = function()
{
return this.replace(/([//s]*$)/g, "");
}
<!--判断字符串是否以指定的字符串开始-->
String.prototype.StartsWith = function(str)
{
return this.substr(0, str.length) == str;
} </script>
</html>
可以把<script type='text/javascript'> </script>之间的内容直接放到JS文件中,就可以调用String的方法。对于var的变量可以先toString()在调用。
比如:
item23 = document.getElementById(documentArray[0]+"_23").value;
Number(item23.toString().movePoint(-2))