当前位置: 首页 > 图文教程 > 网络编程 > Javascript > javascript String 的扩展方法集合

Javascript
innertext , insertadjacentelement , insertadjacenthtml , insertadjacenttext 等区别
JavaScript与C# Windows应用程序交互方法
javascript之解决IE下不渲染的bug
javascript之锁定表格栏位
javascript实现鼠标选取拖动或Ctrl选取拖动
优化网页之快速的呈现我们的网页
javascritp实现input输入框相关限制用法
用javascript实现的激活输入框后隐藏初始内容
CSS代码格式化和压缩的方法与技巧
autocomplete禁止自动完成功能
IE autocomplete internet explorer''s autocomplete
如何快速的呈现我们的网页的技巧整理
计算黄金分割的javascript代码
设置和读取cookie的javascript代码
javascript 复选框选择/全选后特效
实现一个年、月、季度联动SELECT的javascript代码
javascript事件模型代码
WordPress 插件——CoolCode使用方法与下载
用javascript实现画图效果的代码
javascript发表评论或者留言时的展开效果

Javascript 中的 javascript String 的扩展方法集合


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 210 ::
收藏到网摘: n/a

String 的扩展方法集合,可以是javascript对string的功能更多 //获取字符数组
String.prototype.ToCharArray=function()
{
return this.split("");
}
//获取N个相同的字符串
String.prototype.Repeat=function(num)
{
var tmpArr=[];
for(var i=0;i<num;i++) tmpArr.push(this);
return tmpArr.join("");
}
//逆序
String.prototype.Reverse=function()
{
return this.split("").reverse().join("");
}
//测试是否是数字
String.prototype.IsNumeric=function()
{
var tmpFloat=parseFloat(this);
if(isNaN(tmpFloat)) return false;
var tmpLen=this.length-tmpFloat.toString().length;
return tmpFloat+"0".Repeat(tmpLen)==this;
}
//测试是否是整数
String.prototype.IsInt=function()
{
if(this=="NaN") return false;
return this==parseInt(this).toString();
}
// 合并多个空白为一个空白
String.prototype.resetBlank = function()
{
return this.replace(/s+/g," ");
}
// 除去左边空白
String.prototype.LTrim = function()
{
return this.replace(/^s+/g,"");
}
// 除去右边空白
String.prototype.RTrim = function()
{
return this.replace(/s+$/g,"");
}
// 除去两边空白
String.prototype.trim = function()
{
return this.replace(/(^s+)|(s+$)/g,"");
}
// 保留数字
String.prototype.getNum = function()
{
return this.replace(/[^d]/g,"");
}
// 保留字母
String.prototype.getEn = function()
{
return this.replace(/[^A-Za-z]/g,"");
}
// 保留中文
String.prototype.getCn = function()
{
return this.replace(/[^u4e00-u9fa5uf900-ufa2d]/g,"");
}
// 得到字节长度
String.prototype.getRealLength = function()
{
return this.replace(/[^x00-xff]/g,"--").length;
}
// 从左截取指定长度的字串
String.prototype.left = function(n)
{
return this.slice(0,n);
}
// 从右截取指定长度的字串
String.prototype.right = function(n)
{
return this.slice(this.length-n);
}
// HTML编码
String.prototype.HTMLEncode = function()
{
var re = this;
var q1 = [/x26/g,/x3C/g,/x3E/g,/x20/g];
var q2 = ["&","<",">"," "];
for(var i=0;i<q1.length;i++)
re = re.replace(q1[i],q2[i]);
return re;
}
// Unicode转化
String.prototype.ascW = function()
{
var strText = "";
for (var i=0; i<this.length; i++) strText += "&#" + this.charCodeAt(i) + ";";
return strText;
}