sally0156(青草香), gzrex(gzrex), show me the documentation on trim() function in Javascript, thankshere is a faq in comp.lang.javascript:4.16 How do I trim whitespace - LTRIM/RTRIM/TRIM? Using Regular Expressions (JavaScript 1.2/JScript 4+) : String.prototype.LTrim=new Function("return this.replace(/^\\s+/,'')") String.prototype.RTrim=new Function("return this.replace(/\\s+$/,'')") String.prototype.Trim= new Function("return this.replace(/^\\s+|\\s+$/,'')") or for all versions (only trims spaces, not other "whitespace"):
function LTrim(str) { for (var i=0; str.charAt(i)==" "; i++); return str.substring(i,str.length); } function RTrim(str) { for (var i=str.length-1; str.charAt(i)==" "; i--); return str.substring(0,i+1); } function Trim(str) { return LTrim(RTrim(str)); }
//---------------Ltrim(instr)函数----------------------- function Ltrim(instr){ var str=instr+""; if (str.length==0){ return str; } var i=0; while ((i<str.length)&&(str.substring(i,i+1)==" ")&&( i< 2000)) i++; return str.substring(i,str.length); } //--------------Rtrim(str)函数----------------------------- function Rtrim(str){ var instr=str+""; var last_space; var ret; last_space = instr.length; var loop=0; while ((instr.charAt( last_space - 1 ) == " " )&&(last_space > 0)&&( loop< 2000)) { loop++; last_space --; } if (last_space==0){ return "" }else{ return instr.substring( 0, last_space ); } } //--------------------AllTrim(str)函数---------------------------- function AllTrim(str){ return Rtrim(Ltrim(str)); } 绝对好使,记得给分呀!
you must rember if you use the prototype to define the method trim(), you must not allow the string object to null
var a = " hello "; window.execScript("a = trim(a)","vbscript"); alert("/" + a + "/");
这些也太复杂了点吧!这样就行了: function Trim(value){ var res = String(value).replace(/^[\s]+|[\s]+$/g,''); return res; }
javascript 就没有类似trim()之类的函数吗?
String.prototype.RTrim=new Function("return this.replace(/\\s+$/,'')")
String.prototype.Trim=
new Function("return this.replace(/^\\s+|\\s+$/,'')") or for all versions (only trims spaces, not other "whitespace"):
function LTrim(str) {
for (var i=0; str.charAt(i)==" "; i++);
return str.substring(i,str.length);
}
function RTrim(str) {
for (var i=str.length-1; str.charAt(i)==" "; i--);
return str.substring(0,i+1);
}
function Trim(str) {
return LTrim(RTrim(str));
}
function Ltrim(instr){
var str=instr+"";
if (str.length==0){
return str;
}
var i=0;
while ((i<str.length)&&(str.substring(i,i+1)==" ")&&( i< 2000)) i++;
return str.substring(i,str.length);
}
//--------------Rtrim(str)函数-----------------------------
function Rtrim(str){
var instr=str+"";
var last_space;
var ret;
last_space = instr.length;
var loop=0;
while ((instr.charAt( last_space - 1 ) == " " )&&(last_space > 0)&&( loop< 2000)) {
loop++;
last_space --;
}
if (last_space==0){
return ""
}else{
return instr.substring( 0, last_space );
}
}
//--------------------AllTrim(str)函数----------------------------
function AllTrim(str){
return Rtrim(Ltrim(str));
} 绝对好使,记得给分呀!
you must not allow the string object to null
window.execScript("a = trim(a)","vbscript");
alert("/" + a + "/");
function Trim(value){
var res = String(value).replace(/^[\s]+|[\s]+$/g,'');
return res;
}