
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,"");
string.prototype.splitandtrim = function($delimiter, $limit)
{
var $ss = this.split($delimiter, $limit);
for(var $i=0; $i<$ss.length; $i++)
$ss[$i] = $ss[$i].trim();
return $ss;
string.prototype.htmlentities = function () {
return this.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
string.prototype.striptags = function () {
return this.replace(/<([^>]+)>/g,'');
string.prototype.toarray = function() {
return this.split('');
string.prototype.tointarray = function() {
var returnarray = [];
for (var i=0; i<this.length; i++) {
returnarray.push(this.charcodeat(i));
}
return returnarray;
string.prototype.replaceall = function($old, $snew){
return this.replace(new regexp($old,"gm"),$snew);
//變量替換var a = "i love {0}, and you love {1},where are {0}!";a.format("you","me");
string.prototype.format = function(){
var args = arguments;
return this.replace(/\{(\d+)\}/g,function(m,i,o,n){
return args[i];
});
//在字元串末尾追加字元串
string.prototype.append = function($str){
return this.concat($str);
//删除指定索引位置的字元,索引無效将不删除任何字元
string.prototype.deletecharat = function($sindex){
if($sindex<0 || $sindex>=this.length){
return this.valueof();
}else if($sindex==0){
return this.substring(1,this.length);
}else if($sindex==this.length-1){
return this.substring(0,this.length-1);
}else{
return this.substring(0,$sindex)+this.substring($sindex+1);
}
//删除指定索引間的字元串.$sindex和$eindex所在的字元不被删除!依賴deletecharat
string.prototype.deletestring = function($sindex, $eindex){
if($sindex==$eindex){
return this.deletecharat($sindex);
if($sindex>$eindex){
var tindex=$eindex;
$eindex=$sindex;
$sindex=tindex;
}
if($sindex<0)$sindex=0;
if($eindex>this.length-1)$eindex=this.length-1;
return this.substring(0,$sindex+1)+this.substring($eindex,this.length);
//檢查字元串是否以某個字元串(str)結尾
string.prototype.endswith = function($str){
return this.substr(this.length - $str.length) == $str;
//檢查該字元串是否以某個字元串開始
string.prototype.startswith = function(str){
return this.substr(0, str.length) == str;
}
//比較兩個字元串是否相等,不區分大小寫!
string.prototype.equalsignorecase = function($str){
if(this.length!=$str.length){
return false;
var tmp1=this.tolowercase();
var tmp2=$str.tolowercase();
return tmp1==tmp2;
//将指定的字元串插入到指定的位置後面!索引無效将直接追加到字元串的末尾
string.prototype.insert = function($ofset, $str){
if($ofset<0 || $ofset>=this.length-1){
return this.concat($str);
return this.substring(0,$ofset)+$str+this.substring($ofset+1);
//将指定的位置的字元設定為另外指定的字元或字元串.索引無效将直接傳回不做任何處理!
string.prototype.setcharat = function($ofset, $str){
string.prototype.replacelen = function(start, len, replaced) {
if(!len)
return this;
if(start >= this.length)
var returnseg = '';
var returnseg2 = '';
var i = 0;
for (; i < this.length; i++){
var c = this.charat(i);
if(i < start)
returnseg += c;
if(i >= start + len)
returnseg2 += c;
}
return returnseg + replaced + returnseg2;
}
/**
* 擴充基礎類
* 替換字元,這個在替換填入比較有用,比如***天***小時 替換為 <input />天<input />小時
**/
string.prototype.replacechar = function(target, replaced, start) {
if(!target)
if(!start)
start = 0;
var returnval = this.substring(0, start);
var index = 0;
for (var i = start; i < this.length; i++) {
target = typeof target == 'function' ? target.call(this, index) : target;
if (c == target) {
returnval += typeof replaced == 'function' ? replaced.call(this, index) : replaced;
while (i < this.length - 1 && this.charat(i + 1) == c) {
i++;
}
index++;
}else{
returnval += c;
}
return returnval;
//将該字元串反序排列
string.prototype.reverse = function(){
var str="";
for(var i=this.length-1;i>=0;i--){
str=str.concat(this.charat(i));
return str;
//計算長度,每個漢字占兩個長度,英文字元每個占一個長度
string.prototype.uclength = function(){
var len = 0;
for(var i=0;i<this.length;i++){
if(this.charcodeat(i)>255)len+=2;
else len++;
return len;
//在字元串的左邊填充一些特定的字元
string.prototype.lpad = function(len, s) {
var a = new array(this);
var n = (len - this.length);
for ( var i = 0; i < n; i++) {
a.unshift(s);
return a.join("");
//在字元串的右邊填充一些特定的字元
string.prototype.rpad = function(len, s) {
a.push(s);
//把字元串的首字母轉化為大寫
string.prototype.ucwords = function() {
return this.substring(0,1).touppercase().concat(this.substring(1));
string.prototype.contains = function($str) {
return this.indexof($str) > -1 ? true : false;
//将格式為2008-04-02 10:08:44的字元串轉成日期(string對象的值必須為: 2008-04-02 10:08:44)
string.prototype.todate = function(){
var str = this.replace(/-/g,"/");
return (new date(str));
//将原來用字元串表示的十進數轉成十進制浮點數: precision為精度
string.prototype.tofloat = function(precision){
precision = precision || 2;
return parsefloat(this,10).tofixed(precision);
//将原來用字元串表示的十進數轉成十進制整數
string.prototype.toint = function(){
return parseint(this,10).tostring();
//将兩個原來用字元串表示的十進數相加後當作字串傳回 : addend為加數
string.prototype.add = function(addend){
var sum = parsefloat(this,10) + parsefloat(addend,10);
return sum+"";
//十進制轉其他進制代碼如下nextscale為進制 如2,8,16
string.prototype.shiftscale = function(nextscale){
return parsefloat(this).tostring(nextscale);
/**
* 各進制互相轉換
* this對象必須是整數
* @param prescale 原是是幾進制數
* @param nextscale 要轉換成幾進制數
*/
string.prototype.scaleshift = function(prescale,nextscale){
return parseint(this,prescale).tostring(nextscale);
//全角2半角 document.write("ABC 123,我們都是好朋友");
string.prototype.dbc2sbc = function (){
return this.replace(/[\uff01-\uff5e]/g,function(a){return string.fromcharcode(a.charcodeat(0)-65248);}).replace(/\u3000/g," ");
}