字符串操作方法扩展
String.prototype.trim = function () {
return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.toJson = function () {
return $.parseJSON(this);
}
String.prototype.toMoneyString = function (decimalCount) {
var result = "";
var arr = this.split('.');
var i;
for (i = arr[0].length - 3;i>0;i-=3){
result = "," + arr[0].substr(i, 3) + result;
}
result = "¥" + arr[0].substring(0, i+3) + result;
if (arr.length > 1) {
if (decimalCount > arr[1].length)
{
result += "." + arr[1];
for (var j = 0; j < decimalCount - arr[1].length; j++)
{
result += "0";
}
}
else
{
result += "." + arr[1].substring(0, decimalCount);
}
}
return result;
}
String.prototype.getByteCount=function(){
return this.replace(/[^\x00-\xFF]/g, '**').length;
}
String.prototype.cutString = function (length) {
var strCount = this.getByteCount();
if (strCount <= length) {
return str;
}
var sum = 0;
var cutString = "";
for (i = 0; i < this.length; i++) {
var c = this.charAt(i);
sum += c.getByteCount();
cutString += c;
if (sum >= length) {
break;
}
}
return cutString;
}
String.prototype.remove = function (startIndex, count) {
if (startIndex+count >= this.length) {
return this;
}
var str1 = this.substring(0, startIndex);
var str2 = this.substring(startIndex + count, this.length);
return str1 + str2;
}
String.prototype.removeLeft = function (count) {
if (count == undefined) count = 1;
if (this.length >= count) {
return this.substring(count, this.length);
} else {
return this;
}
}
String.prototype.removeRight = function (count) {
if (count == undefined) count = 1;
if (this.length >= count) {
return this.substring(0, this.length-count);
} else {
return this;
}
}
String.prototype.toNumber = function (defaultValue) {
if (isNaN(this)) {
return defaultValue;
} else {
return Number(this);
}
}
String.prototype.toBool = function (defaultValue) {
var str = this.toLowerCase();
if (str == "true") {
return true;
} else if (str == "false") {
return false;
} else {
return defaultValue;
}
}
String.prototype.toDate = function (defaultValue) {
var str = this.replace(/-/g, "/");
var date = new Date(str);
if (date == 'Invalid Date') {
return defaultValue;
} else {
return date;
}
}
String.prototype.isMatch=function (pattern) {
if (this == undefined || pattern == undefined) {
return false;
}
return pattern.test(this);
}
String.prototype.isNull = function () {
if (this == undefined) {
return true;
}
if (this.trim() == "") {
return true;
}
return false;
}
String.prototype.isNotNull = function () {
return !this.isNull();
}
String.prototype.compareLength = function (minLength, maxLength) {
if (minLength != null) {
if (this.length < minLength) {
return false;
}
}
if (maxLength != null) {
if (this.length > maxLength) {
return false;
}
}
return true;
}
String.prototype.isInt = function () {
if (this == undefined) {
return false;
}
return /^((\d)|([1-9]\d+))$/.test(this);
}
String.prototype.isDecimal = function () {
if (this == undefined) {
return false;
}
return /^\d+\.\d+$/.test(this);
}
String.prototype.isNumber = function () {
if (this == undefined) {
return false;
}
return this.isInt()||this.isDecimal();
}
String.prototype.compareValue = function (minValue, maxValue) {
var value = this.toNumber();
if (minValue != null) {
if (value < minValue) {
return false;
}
}
if (maxValue != null) {
if (value > maxValue) {
return false;
}
}
return true;
}
String.prototype.isEmail = function () {
if (this == undefined) {
return false;
}
return /^\w+@\w+(\.[a-zA-Z]{2,3}){1,2}$/.test(this);
}
String.prototype.isPhone = function () {
if (this == undefined) {
return false;
}
return /^(\d{3,4}-)?\d{7,8}(-\d{3,4})?$/.test(this);
}
String.prototype.isTelephone = function () {
if (this == undefined) {
return false;
}
return /^1\d{10}$/.test(this);
}
String.prototype.isChinese = function () {
if (this == undefined) {
return false;
}
return /^[\u4e00-\u9fa5]+$/.test(this);
}
String.prototype.isUrl = function () {
if (this == undefined) {
return false;
}
var reg = /^http:\/\/(\S+\/?)*(\w+(\?\w+=[^&=\s]+(&\w+=[^&=\s]+)*)?)?$/;
return reg.test(this);
}
String.prototype.isDate = function () {
return this.toDate(1) != 1;
}
String.prototype.compareDate = function (minValue,maxValue) {
var dateValue = this.toDate();
if (minValue != null) {
if (typeof minValue == "string") {
minValue=minValue.toDate();
}
if (dateValue < minValue) {
return false;
}
}
if (maxValue != null) {
if (typeof maxValue == "string") {
maxValue = maxValue.toDate();
}
if (dateValue > maxValue) {
return false;
}
}
return true;
}