js截取方式
1、区别汉字和英文
代码
function widthCheck(name, maxLength){
if(!maxLength){
maxLength = 20;
}
if(name==null||name.length<1){
return ["", ""];
}
var w = 0;//字符串长度,一个汉字长度为2
var s = 0;//汉字个数
var p = false;//判断字符串当前循环的前一个字符是否为汉字
var b = false;//判断字符串当前循环的字符是否为汉字
var nameSub;
for (var i=0; i<name.length; i++) {
if(i>1 && b==false){
p = false;
}
if(i>1 && b==true){
p = true;
}
var c = name.charCodeAt(i);
//单字节加1
if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) {
w++;
b = false;
}else {
w+=2;
s++;
b = true;
}
if(w>maxLength && i<=name.length-1){
if(b==true && p==true){
nameSub = name.substring(0,i-2)+"...";
}
if(b==false && p==false){
nameSub = name.substring(0,i-3)+"...";
}
if(b==true && p==false){
nameSub = name.substring(0,i-2)+"...";
}
if(p==true){
nameSub = name.substring(0,i-2)+"...";
}
break;
}
}
if(w<=maxLength){
return [name, ""];
}
return [nameSub, name];
}
js中使用:
var exampleNameAndTitle = widthCheck(catexampleName,18);//截取18个字节
var exampleNameAndTitle = widthCheck(catexampleName);//截取20个字节
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/carefree31441/archive/2009/09/10/4538166.aspx
if(!maxLength){
maxLength = 20;
}
if(name==null||name.length<1){
return ["", ""];
}
var w = 0;//字符串长度,一个汉字长度为2
var s = 0;//汉字个数
var p = false;//判断字符串当前循环的前一个字符是否为汉字
var b = false;//判断字符串当前循环的字符是否为汉字
var nameSub;
for (var i=0; i<name.length; i++) {
if(i>1 && b==false){
p = false;
}
if(i>1 && b==true){
p = true;
}
var c = name.charCodeAt(i);
//单字节加1
if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) {
w++;
b = false;
}else {
w+=2;
s++;
b = true;
}
if(w>maxLength && i<=name.length-1){
if(b==true && p==true){
nameSub = name.substring(0,i-2)+"...";
}
if(b==false && p==false){
nameSub = name.substring(0,i-3)+"...";
}
if(b==true && p==false){
nameSub = name.substring(0,i-2)+"...";
}
if(p==true){
nameSub = name.substring(0,i-2)+"...";
}
break;
}
}
if(w<=maxLength){
return [name, ""];
}
return [nameSub, name];
}
js中使用:
var exampleNameAndTitle = widthCheck(catexampleName,18);//截取18个字节
var exampleNameAndTitle = widthCheck(catexampleName);//截取20个字节
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/carefree31441/archive/2009/09/10/4538166.aspx
2、不区分汉字和英文,只按字符个数截取
代码
function shorten(categoryName, maxLength){
if(!maxLength){
maxLength = 20;
}
if(categoryName==null||categoryName.length<1){
return ["", ""];
}
if(categoryName.length<maxLength){
return [categoryName, ""];
}
return [categoryName.substring(0, maxLength-3)+"...", categoryName];
}
js中使用:
var exampleNameAndTitle = shorten(catexampleName,18);//截取18个字符
var exampleNameAndTitle = shorten(catexampleName);//截取20个字符
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/carefree31441/archive/2009/09/10/4538166.aspx
if(!maxLength){
maxLength = 20;
}
if(categoryName==null||categoryName.length<1){
return ["", ""];
}
if(categoryName.length<maxLength){
return [categoryName, ""];
}
return [categoryName.substring(0, maxLength-3)+"...", categoryName];
}
js中使用:
var exampleNameAndTitle = shorten(catexampleName,18);//截取18个字符
var exampleNameAndTitle = shorten(catexampleName);//截取20个字符
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/carefree31441/archive/2009/09/10/4538166.aspx