/**
* 获取字符串对应的字节数
*/
String.prototype.getByte = function() {
return this.replace(/[^x00-xff]/g, '00').length;
}
/**
* 截取字符串长度
*/
String.prototype.subString = function(len, bt, aso) {
var str = this;
if(bt) {
if(str.getByte() > len) {
var charlen = 0, chars = '';
for(var i = 0; i < str.length; i++) {
var c = str.substring(i, i + 1);
if(/^[^x00-xff]$/.test(c)) {
charlen += 2;
} else {
charlen += 1;
}
if(charlen <= len) {
chars += c;
} else {
break;
}
}
str = chars;
if(aso) {
str += aso;
}
}
} else {
if(this.length > len) {
str = str.substring(0, len);
if(aso) {
str += aso;
}
}
}
return str;
}
/**
* 去除字符串左边空白字符
*/
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, '');
}
/**
* 把一些预定义的字符转换为HTML实体
*/
String.prototype.filter = function() {
var str = this, result = [], c = '';
for(var i = 0, len = str.length; i < len; i++) {
c = str.charAt(i);
switch (c) {
case '<':
result[i] = "<";
break;
case '>':
result[i] = ">";
break;
case '"':
result[i] = """;
break;
case '\'':
result[i] = "'";
break;
case '%':
result[i] = "%";
break;
case ';':
result[i] = ";";
break;
case '(':
result[i] = "(";
break;
case ')':
result[i] = ")";
break;
case '&':
result[i] = "&";
break;
case '+':
result[i] = "+";
break;
case ' ':
result[i] = " ";
break;
default:
result[i] = c;
break;
}
}
return result.join('');
}
/**
* 在字符串中的每个新行\n之前插入HTML换行符<br />
*/
String.prototype.nl2br = function() {
return this.replace(/\n/g, '<br />');
}
String.prototype.colorHex=function(){
var str=this.valueOf();
str=str.substring();
}
/**
* @param {Boolean} isComponent 是否获取分量,是的话以 [r,g,b,a]的形式返回
*/
String.prototype.colorRgba=function(isComponent){
var str=this.valueOf();
if(str.length<7){
throw new Error('颜色值不对');
}
str=str.substr(1);
var alpha=1, r=0, g=0, b=0;
if(str.length==8){
alpha=parseInt('0x'+str.substr(6))/255;
}
r=parseInt('0x'+str.substr(0,2));
g=parseInt('0x'+str.substr(2,2));
b=parseInt('0x'+str.substr(4,2));
if(isComponent){
return [r,g,b,alpha];
}
return 'RGBA('+ r+','+g+','+b+','+alpha +')';
}
* 获取字符串对应的字节数
*/
String.prototype.getByte = function() {
return this.replace(/[^x00-xff]/g, '00').length;
}
/**
* 截取字符串长度
*/
String.prototype.subString = function(len, bt, aso) {
var str = this;
if(bt) {
if(str.getByte() > len) {
var charlen = 0, chars = '';
for(var i = 0; i < str.length; i++) {
var c = str.substring(i, i + 1);
if(/^[^x00-xff]$/.test(c)) {
charlen += 2;
} else {
charlen += 1;
}
if(charlen <= len) {
chars += c;
} else {
break;
}
}
str = chars;
if(aso) {
str += aso;
}
}
} else {
if(this.length > len) {
str = str.substring(0, len);
if(aso) {
str += aso;
}
}
}
return str;
}
/**
* 去除字符串左边空白字符
*/
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, '');
}
/**
* 把一些预定义的字符转换为HTML实体
*/
String.prototype.filter = function() {
var str = this, result = [], c = '';
for(var i = 0, len = str.length; i < len; i++) {
c = str.charAt(i);
switch (c) {
case '<':
result[i] = "<";
break;
case '>':
result[i] = ">";
break;
case '"':
result[i] = """;
break;
case '\'':
result[i] = "'";
break;
case '%':
result[i] = "%";
break;
case ';':
result[i] = ";";
break;
case '(':
result[i] = "(";
break;
case ')':
result[i] = ")";
break;
case '&':
result[i] = "&";
break;
case '+':
result[i] = "+";
break;
case ' ':
result[i] = " ";
break;
default:
result[i] = c;
break;
}
}
return result.join('');
}
/**
* 在字符串中的每个新行\n之前插入HTML换行符<br />
*/
String.prototype.nl2br = function() {
return this.replace(/\n/g, '<br />');
}
String.prototype.colorHex=function(){
var str=this.valueOf();
str=str.substring();
}
/**
* @param {Boolean} isComponent 是否获取分量,是的话以 [r,g,b,a]的形式返回
*/
String.prototype.colorRgba=function(isComponent){
var str=this.valueOf();
if(str.length<7){
throw new Error('颜色值不对');
}
str=str.substr(1);
var alpha=1, r=0, g=0, b=0;
if(str.length==8){
alpha=parseInt('0x'+str.substr(6))/255;
}
r=parseInt('0x'+str.substr(0,2));
g=parseInt('0x'+str.substr(2,2));
b=parseInt('0x'+str.substr(4,2));
if(isComponent){
return [r,g,b,alpha];
}
return 'RGBA('+ r+','+g+','+b+','+alpha +')';
}