日期:2014-05-16  浏览次数:20396 次

js String 方法
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.htmlEncode = function() {
	return this.replace(/&/g, "&amp;").replace(/>/g, "&gt;").replace(/</g,
			"&lt;").replace(/"/g, "&quot;");
}
String.prototype.htmlDecode = function() {
	return this.replace(/&amp;/g, "&").replace(/&gt;/g, ">").replace(/&lt;/g,
			"<").replace(/&quot;/g, '"');
}
//获得字符串长度,将中文转换为两个字符长度
String.prototype.len= function ()          
{                 
return       this .replace(/[^\x00-\xff]/g, "rr" ).length;          
}       
//截取中英文字符串
String.prototype.sub =  function (n) {    
 var  r = /[^\x00-\xff]/g;    
 if ( this .replace(r,  "mm" ).length <= n)  return   this ;    
 var  m = Math.floor(n/2);    
 for ( var  i=m; i< this .length; i++) {    
 if ( this .substr(0, i).replace(r,  "mm" ).length>=n) {    
   return   this .substr(0, i) + "..." ; }    
 }  return   this ;   
};  
/×××以上资源非原创,乃网上多出收集而来,方便大家使用·····×××/

1 楼 yangpanwww 2010-07-27  
   O(∩_∩)O哈哈~。。不错