字符串处理工具类
2021/4/24 10:26:54
本文主要是介绍字符串处理工具类,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
字符串处理工具类
1、具体页面代码
/**
- 字符串处理工具类
/
var StrUtil = {
/*-
方法作用:计算距离
-
@param: {距离m} num
-
@return: {距离}
/
CalcDistance: function(num) {
if (Number(num) > 1000) {
return Number(Number(num) / 1000).toFixed(2)+‘km’;
}
if(Number(num) ==-1){
return ‘未知’
}
return Number(num).toFixed(2)+‘m’;
},
/* -
方法作用:格式为610521********4575
-
@param: {身份证号码} e
-
@return: {6105214575}
/
GetIDNumber: function(e) {
var idCards;
var numberSix = e.toString().substring(0, 6);
var numberfour = e.toString().substring(e.toString().length - 4, e.toString().length);
switch (e.toString().length) {
case 18: //18位补8个;
idCards = numberSix + '’ + numberfour;
break;
case 15: //15位补5个*;
idCards = numberSix + '***’ + numberfour;
break;
}
return idCards;
},
/ -
方法作用:【base64解密】
-
@param: str{date} 需要解密的字符串
-
@returns: {utf-8的字符串,…}
*/
Base64decode: function(str) {
var base64DecodeChars = new Array(
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);var c1, c2, c3, c4;
var i, len, out;len = str.length;
i = 0;
out = “”;
while (i < len) {
/* c1 */
do {
c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
} while (i < len && c1 == -1);
if (c1 == -1)
break;/* c2 */ do { c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff]; } while (i < len && c2 == -1); if (c2 == -1) break; out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4)); /* c3 */ do { c3 = str.charCodeAt(i++) & 0xff; if (c3 == 61) return out; c3 = base64DecodeChars[c3]; } while (i < len && c3 == -1); if (c3 == -1) break; out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2)); /* c4 */ do { c4 = str.charCodeAt(i++) & 0xff; if (c4 == 61) return out; c4 = base64DecodeChars[c4]; } while (i < len && c4 == -1); if (c4 == -1) break; out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
}
return Base64.Utf8to16(out);
},
/** -
方法作用:【将字符串编译为utf-8的字符串】
-
@param: str{date} 需要编译的字符串
-
@returns: {utf-8的字符串,…}
*/
Utf8to16: function(str) {
var out, i, len, c;
var char2, char3;out = “”;
len = str.length;
i = 0;
while (i < len) {
c = str.charCodeAt(i++);
switch (c >> 4) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
// 0xxxxxxx
out += str.charAt(i - 1);
break;
case 12:
case 13:
// 110x xxxx 10xx xxxx
char2 = str.charCodeAt(i++);
out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
break;
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = str.charCodeAt(i++);
char3 = str.charCodeAt(i++);
out += String.fromCharCode(((c & 0x0F) << 12) |
((char2 & 0x3F) << 6) |
((char3 & 0x3F) << 0));
break;
}
}return out;
},
/** -
方法作用:【截取字符串】
-
@param: value{date} 需要截取的字符串
-
@param: index{date} 从第几个索引截取
-
@param: count{date} 截取几位
-
@param: symbol{date} 截取完之后补的符号 例如:… (省略号)
-
@returns: {字符串,…}
/
GetStrSubstring: function(value, index, count, symbol) {
if (value.length > count) {
var str = value.substring(index, count) + symbol.toString();
return str;
} else {
return value;
}
},
/ -
判断字符串是否为空
-
@param: str 传入的字符串
-
@returns: {}
/
IsEmpty: function(str) {
if (str != null && str.length > 0 && str != undefined && str != ‘’) {
return true;
} else {
return false;
}
},
/ -
判断两个字符串子否相同
-
@param: str1
-
@param: str2
-
@returns: {Boolean}
/
IsEquals: function(str1, str2) {
if (str1 == str2) {
return true;
} else {
return false;
}
},
/ -
忽略大小写判断字符串是否相同
-
@param: str1
-
@param :str2
-
@returns: {Boolean}
/
IsEqualsIgnorecase: function(str1, str2) {
if (str1.toUpperCase() == str2.toUpperCase()) {
return true;
} else {
return false;
}
},
/* -
判断是否是数字
-
@param: value
-
@returns :{Boolean}
/
IsNum: function(value) {
if (value != null && value.length > 0 && isNaN(value) == false) {
return true;
} else {
return false;
}
},
/* -
判断是否是中文
-
@param: str
-
@returns: {Boolean}
/
IsChine: function(str) {
var reg = /^([u4E00-u9FA5]|[uFE30-uFFA0])$/;
if (reg.test(str)) {
return false;
}
return true;
},
/** -
验证是否为图片
-
@param: {str} fileName
/
tmCheckImage: function(fileName) {
return /(gif|jpg|jpeg|png|GIF|JPG|PNG)$/ig.test(fileName);
},
/* -
验证是否为视频
-
@param: {str} fileName
/
tmCheckVideo: function(fileName) {
return /(mp4|mp3|flv|wav)$/ig.test(fileName);
},
/* -
验证是否为文档
-
@param: {str} fileName
/
tmCheckDocument: function(fileName) {
return /(doc|docx|xls|xlsx|pdf|txt|ppt|pptx|rar|zip|html|jsp|sql|htm|shtml|xml)$/ig.test(fileName);
},
/* -
数字千分位分开
-
@param: {Object} str
*/
formatNum: function(str) {
var newStr = “”;
var count = 0;if (str.indexOf(".") == -1) {
for (var i = str.length - 1; i >= 0; i–) {
if (count % 3 == 0 && count != 0) {
newStr = str.charAt(i) + “,” + newStr;
} else {
newStr = str.charAt(i) + newStr;
}
count++;
}
str = newStr + “.00”; //自动补小数点后两位
console.log(str)
} else {
for (var i = str.indexOf(".") - 1; i >= 0; i–) {
if (count % 3 == 0 && count != 0) {
newStr = str.charAt(i) + “,” + newStr; //碰到3的倍数则加上“,”号
} else {
newStr = str.charAt(i) + newStr; //逐个字符相接起来
}
count++;
}
str = newStr + (str + “00”).substr((str + “00”).indexOf("."), 3);
console.log(str)
}
},
/** -
将数字转换为大写金额
-
@param: {Object} Num
/
change_to_Chinese: function(Num) {
//判断如果传递进来的不是字符的话转换为字符
if (typeof Num == “number”) {
Num = new String(Num);
};
Num = Num.replace(/,/g, “”) //替换tomoney()中的“,”
Num = Num.replace(/ /g, “”) //替换tomoney()中的空格
Num = Num.replace(/¥/g, “”) //替换掉可能出现的¥字符
if (isNaN(Num)) { //验证输入的字符是否为数字
//alert(“请检查小写金额是否正确”);
return “”;
};
//字符处理完毕后开始转换,采用前后两部分分别转换
var part = String(Num).split(".");
var newchar = “”;
//小数点前进行转化
for (var i = part[0].length - 1; i >= 0; i–) {
if (part[0].length > 10) {
return “”;
//若数量超过拾亿单位,提示
}
var tmpnewchar = “”
var perchar = part[0].charAt(i);
switch (perchar) {
case “0”:
tmpnewchar = “零” + tmpnewchar;
break;
case “1”:
tmpnewchar = “壹” + tmpnewchar;
break;
case “2”:
tmpnewchar = “贰” + tmpnewchar;
break;
case “3”:
tmpnewchar = “叁” + tmpnewchar;
break;
case “4”:
tmpnewchar = “肆” + tmpnewchar;
break;
case “5”:
tmpnewchar = “伍” + tmpnewchar;
break;
case “6”:
tmpnewchar = “陆” + tmpnewchar;
break;
case “7”:
tmpnewchar = “柒” + tmpnewchar;
break;
case “8”:
tmpnewchar = “捌” + tmpnewchar;
break;
case “9”:
tmpnewchar = “玖” + tmpnewchar;
break;
}
switch (part[0].length - i - 1) {
case 0:
tmpnewchar = tmpnewchar + “元”;
break;
case 1:
if (perchar != 0) tmpnewchar = tmpnewchar + “拾”;
break;
case 2:
if (perchar != 0) tmpnewchar = tmpnewchar + “佰”;
break;
case 3:
if (perchar != 0) tmpnewchar = tmpnewchar + “仟”;
break;
case 4:
tmpnewchar = tmpnewchar + “万”;
break;
case 5:
if (perchar != 0) tmpnewchar = tmpnewchar + “拾”;
break;
case 6:
if (perchar != 0) tmpnewchar = tmpnewchar + “佰”;
break;
case 7:
if (perchar != 0) tmpnewchar = tmpnewchar + “仟”;
break;
case 8:
tmpnewchar = tmpnewchar + “亿”;
break;
case 9:
tmpnewchar = tmpnewchar + “拾”;
break;
}
var newchar = tmpnewchar + newchar;
}
//小数点之后进行转化
if (Num.indexOf(".") != -1) {
if (part[1].length > 2) {
// alert(“小数点之后只能保留两位,系统将自动截断”);
part[1] = part[1].substr(0, 2)
}
for (i = 0; i < part[1].length; i++) {
tmpnewchar = “”
perchar = part[1].charAt(i)
switch (perchar) {
case “0”:
tmpnewchar = “零” + tmpnewchar;
break;
case “1”:
tmpnewchar = “壹” + tmpnewchar;
break;
case “2”:
tmpnewchar = “贰” + tmpnewchar;
break;
case “3”:
tmpnewchar = “叁” + tmpnewchar;
break;
case “4”:
tmpnewchar = “肆” + tmpnewchar;
break;
case “5”:
tmpnewchar = “伍” + tmpnewchar;
break;
case “6”:
tmpnewchar = “陆” + tmpnewchar;
break;
case “7”:
tmpnewchar = “柒” + tmpnewchar;
break;
case “8”:
tmpnewchar = “捌” + tmpnewchar;
break;
case “9”:
tmpnewchar = “玖” + tmpnewchar;
break;
}
if (i == 0) tmpnewchar = tmpnewchar + “角”;
if (i == 1) tmpnewchar = tmpnewchar + “分”;
newchar = newchar + tmpnewchar;
}
}
//替换所有无用汉字
while (newchar.search(“零零”) != -1) {
newchar = newchar.replace(“零零”, “零”);
newchar = newchar.replace(“零亿”, “亿”);
newchar = newchar.replace(“亿万”, “亿”);
newchar = newchar.replace(“零万”, “万”);
newchar = newchar.replace(“零元”, “元”);
newchar = newchar.replace(“零角”, “”);
newchar = newchar.replace(“零分”, “”);
if (newchar.charAt(newchar.length - 1) == “元”) {
newchar = newchar + “整”
}
return newchar;
}
},
/* -
json对象转换为json字符串
-
@param: obj
-
@returns: {*}
*/
jsonToString: function(obj) {
var THIS = this;
switch (typeof(obj)) {
case ‘string’:
return ‘"’ + obj.replace(/(["\])/g, ‘\$1’) + ‘"’;
case ‘array’:
return ‘[’ + obj.map(THIS.jsonToString).join(’,’) + ‘]’;
case ‘object’:
if (obj instanceof Array) {
var strArr = [];
var len = obj.length;
for (var i = 0; i < len; i++) {
strArr.push(THIS.jsonToString(obj[i]));
}
return ‘[’ + strArr.join(’,’) + ‘]’;
} else if (obj == null) {
return ‘null’;} else { var string = []; for (var property in obj) string.push(THIS.jsonToString(property) + ':' + THIS.jsonToString(obj[property])); return '{' + string.join(',') + '}'; } case 'number': return obj; case false: return obj;
}
},
/** -
获取浏览器url中的参数值
-
@param: {Object} name
/
getURLParam: function(name) {
return decodeURIComponent((new RegExp(’[?|&]’ + name + ‘=’ + ‘([^&;]+?)(&|#|;|$)’, “ig”).exec(location
.search) || [,
“”
])[1].replace(/+/g, ‘%20’)) || null;
},
/* -
获取十六进制随机颜色
-
@returns: {string}
/
getRandomColor: function() {
return ‘#’ + (function(h) {
return new Array(7 - h.length).join(“0”) + h;
})((Math.random() * 0x1000000 << 0).toString(16));
},
/* -
判断是否是偶数
-
@returns: {true 是偶数 false 不是}
*/
isEvenNumbers: function(num) {
return num % 2 == 0 ? true : false;
}
}
-
这篇关于字符串处理工具类的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-27OpenFeign服务间调用学习入门
- 2024-12-27OpenFeign服务间调用学习入门
- 2024-12-27OpenFeign学习入门:轻松掌握微服务通信
- 2024-12-27OpenFeign学习入门:轻松掌握微服务间的HTTP请求
- 2024-12-27JDK17新特性学习入门:简洁教程带你轻松上手
- 2024-12-27JMeter传递token学习入门教程
- 2024-12-27JMeter压测学习入门指南
- 2024-12-27JWT单点登录学习入门指南
- 2024-12-27JWT单点登录原理学习入门
- 2024-12-27JWT单点登录原理学习入门