欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

常用的String原型

程序员文章站 2022-06-25 08:17:21
对于常用的字符串原型的举例 在字符串末尾追加字符串 String.prototype.append = function (str) { return this.concat(str);} 删除指定索引位置的字符,索引无效将不删除任何字符 String.prototype.deleteCharAt ......

对于常用的字符串原型的举例

 在字符串末尾追加字符串 
string.prototype.append = function (str) {
return this.concat(str);
}

 删除指定索引位置的字符,索引无效将不删除任何字符 
string.prototype.deletecharat = function (index) {
if (index < 0 || index >= this.length) {
return this.valueof();
}
else if (index == 0) {
return this.substring(1, this.length);
}
else if (index == this.length - 1) {
return this.substring(0, this.length - 1);
}
else {
return this.substring(0, index) + this.substring(index + 1);
}
}
 删除指定索引区间的字符串 
string.prototype.deletestring = function (start, end) {
if (start == end) {
return this.deletecharat(start);
}
else {
if (start > end) {
var temp = start;
start = end;
end = temp;
}
if (start < 0) {
start = 0;
}
if (end > this.length - 1) {
end = this.length - 1;
}
return this.substring(0, start) + this.substring(end +1 , this.length);
}
}
 检查字符串是否以substr结尾 
string.prototype.endwith = function (substr) {
if (substr.length > this.length) {
return false;
}
else {
return (this.lastindexof(substr) == (this.length - substr.length)) ? true : false;
}
}
 比较两个字符串是否相等,也可以直接用 == 进行比较 
string.prototype.equal = function (str) {
if (this.length != str.length) {
return false;
}
else {
for (var i = 0; i < this.length; i++) {
if (this.charat(i) != str.charat(i)) {
return false;
}
}
return true;
}
}
 比较两个字符串是否相等,不区分大小写 
string.prototype.equalignorecase = function (str) {
var temp1 = this.tolowercase();
var temp2 = str.tolowercase();
return temp1.equal(temp2);
}
 将指定的字符串插入到指定的位置后面,索引无效将直接追加到字符串的末尾 
string.prototype.insert = function (ofset, substr) {
if (ofset < 0 || ofset >= this.length - 1) {
return this.append(substr);
}
return this.substring(0, ofset + 1) + substr + this.substring(ofset + 1);
}
 判断字符串是否数字串 
string.prototype.isallnumber = function () {
for (var i = 0; i < this.length; i++) {
if (this.charat(i) < '0' || this.charat(i) > '9') {
return false;
}
}
return true;
}
 将字符串反序排列 
string.prototype.reserve = function () {
var temp = "";
for (var i = this.length - 1; i >= 0; i--) {
temp = temp.concat(this.charat(i));
}
return temp;
}
 将指定的位置的字符设置为另外指定的字符或字符串.索引无效将直接返回不做任何处理 
string.prototype.setcharat = function (index, substr) {
if (index < 0 || index > this.length - 1) {
return this.valueof();
}
return this.substring(0, index) + substr + this.substring(index+1);
}
 检查字符串是否以substr开头 
string.prototype.startwith = function (substr) {
if (substr.length > this.length) {
return false;
}
return (this.indexof(substr) == 0) ? true : false;
}
 计算长度,每个汉字占两个长度,英文字符每个占一个长度 
string.prototype.charlength = function () {
var temp = 0;
for (var i = 0; i < this.length; i++) {
if (this.charcodeat(i) > 255) {
temp += 2;
}
else {
temp += 1;
}
}
return temp;
}
string.prototype.charlengthreg = function () {
return this.replace(/[^\x00-\xff]/g, "**").length;
}
 去掉首尾空格 
string.prototype.trim = function () {
return this.replace(/(^\s*)|(\s*$)/g, "");
}
 测试是否是数字 
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();
}
 获取n个相同的字符串 
string.prototype.repeat = function (num) {
var tmparr = [];
for (var i = 0; i < num; i++) tmparr.push(this);
return tmparr.join("");
}
 除去左边空白  
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);
}
 删除首尾空格  
string.prototype.trim = function() {
return this.replace(/(^\s*)|(\s*$)/g, "");
}

 统计指定字符出现的次数  
string.prototype.occurs = function(ch) {
// var re = eval("/[^"+ch+"]/g");
// return this.replace(re, "").length;
return this.split(ch).length-1;
}

 检查是否由数字组成 
string.prototype.isdigit = function() {
var s = this.trim();
return (s.replace(/\d/g, "").length == 0);
}

 检查是否由数字字母和下划线组成 
string.prototype.isalpha = function() {
return (this.replace(/\w/g, "").length == 0);
}

检查是否为数 
string.prototype.isnumber = function() {
var s = this.trim();
return (s.search(/^[+-]?[0-9.]*$/) >= 0);
}

返回字节数
string.prototype.lenb = function() {
return this.replace(/[^\x00-\xff]/g,"**").length;
}

检查是否包含汉字
string.prototype.isinchinese = function() {
return (this.length != this.replace(/[^\x00-\xff]/g,"**").length);
}

简单的email检查 
string.prototype.isemail = function() {
 var strr;
var mail = this;
 var re = /(\w+@\w+\.\w+)(\.{0,1}\w*)(\.{0,1}\w*)/i;
 re.exec(mail);
 if(regexp.$3!="" && regexp.$3!="." && regexp.$2!=".")
strr = regexp.$1+regexp.$2+regexp.$3;
 else
  if(regexp.$2!="" && regexp.$2!=".")
strr = regexp.$1+regexp.$2;
  else
 strr = regexp.$1;
 return (strr==mail);
}

简单的日期检查,成功返回日期对象  
string.prototype.isdate = function() {
var p;
var re1 = /(\d{4})[年./-](\d{1,2})[月./-](\d{1,2})[日]?$/;
var re2 = /(\d{1,2})[月./-](\d{1,2})[日./-](\d{2})[年]?$/;
var re3 = /(\d{1,2})[月./-](\d{1,2})[日./-](\d{4})[年]?$/;
if(re1.test(this)) {
p = re1.exec(this);
return new date(p[1],p[2],p[3]);
}
if(re2.test(this)) {
p = re2.exec(this);
return new date(p[3],p[1],p[2]);
}
if(re3.test(this)) {
p = re3.exec(this);
return new date(p[3],p[1],p[2]);
}
return false;
}

检查是否有列表中的字符字符
string.prototype.isinlist = function(list) {
var re = eval("/["+list+"]/");
return re.test(this);
}

系统中js的扩展函数 

/清除两边的空格
string.prototype.trim = function() {
return this.replace(/(^\s*)|(\s*$)/g, '');
};
 合并多个空白为一个空白
string.prototype.resetblank = function() {
var regex = /\s+/g;
return this.replace(regex, ' ');
};

保留数字
string.prototype.getnum = function() {
var regex = /[^\d]/g;
return this.replace(regex, '');
};

保留中文
string.prototype.getcn = function() {
var regex = /[^\u4e00-\u9fa5\uf900-\ufa2d]/g;
return this.replace(regex, '');
};

string转化为number
string.prototype.toint = function() {
return isnan(parseint(this)) ? this.tostring() : parseint(this);
};

 得到字节长度
string.prototype.getlen = function() {
var regex = /^[\u4e00-\u9fa5\uf900-\ufa2d]+$/;
if (regex.test(this)) {
return this.length * 2;
} else {
var omatches = this.match(/[\x00-\xff]/g);
var olength = this.length * 2 - omatches.length;
return olength;
}
};

 获取文件全名
string.prototype.getfilename = function() {
var regex = /^.*\/([^\/\?]*).*$/;
return this.replace(regex, '$1');
};

 数字补零
number.prototype.lenwithzero = function(ocount) {
var strtext = this.tostring();
while (strtext.length < ocount) {
strtext = '0' + strtext;
}
return strtext;
};

unicode还原
number.prototype.chrw = function() {
return string.fromcharcode(this);
};

 数字数组由小到大排序
array.prototype.min2max = function() {
var ovalue;
for (var i = 0; i < this.length; i++) {
for (var j = 0; j <= i; j++) {
if (this[i] < this[j]) {
ovalue = this[i];
this[i] = this[j];
this[j] = ovalue;
}
}
}
return this;
};

 数字数组由大到小排序
array.prototype.max2min = function() {
var ovalue;
for (var i = 0; i < this.length; i++) {
for (var j = 0; j <= i; j++) {
if (this[i] > this[j]) {
ovalue = this[i];
this[i] = this[j];
this[j] = ovalue;
}
}
}
return this;
};

 获得数字数组中最大项
array.prototype.getmax = function() {
var ovalue = 0;
for (var i = 0; i < this.length; i++) {
if (this[i] > ovalue) {
ovalue = this[i];
}
}
return ovalue;
};

 获得数字数组中最小项
array.prototype.getmin = function() {
var ovalue = 0;
for (var i = 0; i < this.length; i++) {
if (this[i] < ovalue) {
ovalue = this[i];
}
}
return ovalue;
};

 获取当前时间的中文形式
date.prototype.getcndate = function() {
var odatetext = '';
odatetext += this.getfullyear().lenwithzero(4) + new number(24180).chrw();
odatetext += this.getmonth().lenwithzero(2) + new number(26376).chrw();
odatetext += this.getdate().lenwithzero(2) + new number(26085).chrw();
odatetext += this.gethours().lenwithzero(2) + new number(26102).chrw();
odatetext += this.getminutes().lenwithzero(2) + new number(20998).chrw();
odatetext += this.getseconds().lenwithzero(2) + new number(31186).chrw();
odatetext += new number(32).chrw() + new number(32).chrw() + new number(26143).chrw() + new number(26399).chrw() + new string('26085199682010819977222352011620845').substr(this.getday() * 5, 5).toint().chrw();
return odatetext;
};
扩展date格式化
date.prototype.format = function(format) {
var o = {
"m+": this.getmonth() + 1, //月份
"d+": this.getdate(), //日
"h+": this.gethours() % 12 == 0 ? 12 : this.gethours() % 12, //小时
"h+": this.gethours(), //小时
"m+": this.getminutes(), //分
"s+": this.getseconds(), //秒
"q+": math.floor((this.getmonth() + 3) / 3), //季度
"s": this.getmilliseconds() //毫秒
};
var week = {
"0": "\u65e5",
"1": "\u4e00",
"2": "\u4e8c",
"3": "\u4e09",
"4": "\u56db",
"5": "\u4e94",
"6": "\u516d"
};
if (/(y+)/.test(format)) {
format = format.replace(regexp.$1, (this.getfullyear() + "").substr(4 - regexp.$1.length));
}
if (/(e+)/.test(format)) {
format = format.replace(regexp.$1, ((regexp.$1.length > 1) ? (regexp.$1.length > 2 ? "\u661f\u671f" : "\u5468") : "") + week[this.getday() + ""]);
}
for (var k in o) {
if (new regexp("(" + k + ")").test(format)) {
format = format.replace(regexp.$1, (regexp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
}
}
return format;
}
date.prototype.diff = function(interval, objdate) {
 若参数不足或 objdate 不是日期类型則回传 undefined
if (arguments.length < 2 || objdate.constructor != date) { return undefined; }
switch (interval) {
//计算秒差
case 's': return parseint((objdate - this) / 1000);
//计算分差
case 'n': return parseint((objdate - this) / 60000);
//计算時差
case 'h': return parseint((objdate - this) / 3600000);
//计算日差
case 'd': return parseint((objdate - this) / 86400000);
//计算周差
case 'w': return parseint((objdate - this) / (86400000 * 7));
//计算月差
case 'm': return (objdate.getmonth() + 1) + ((objdate.getfullyear() - this.getfullyear()) * 12) - (this.getmonth() + 1);
//计算年差
case 'y': return objdate.getfullyear() - this.getfullyear();
//输入有误
default: return undefined;
}
};

 检测是否为空
object.prototype.isnullorempty = function() {
var obj = this;
var flag = false;
if (obj == null || obj == undefined || typeof (obj) == 'undefined' || obj == '') {
flag = true;
} else if (typeof (obj) == 'string') {
obj = obj.trim();
if (obj == '') {//为空
flag = true;
} else {//不为空
obj = obj.touppercase();
if (obj == 'null' || obj == 'undefined' || obj == '{}') {
flag = true;
}
}
}
else {
flag = false;
}
return flag;
};