由于接到一任务,大致是这样子的:一个弹页面里面要显示一段文字(多国字符),弹出页面的宽度是定死的。客户不希望文字长度过长时,下面出现水平的拉条。这就关系到一个自动换行的问题。
由于中文,日文,韩文等一般占2个字节,英文一般占1个字节,所以要换行,首先要计算长度。只有在字节长度相同的情况下换行,同时又要注意不能将中文字拆开了,否则就会是乱码了。
经过一番努力,这个问题终于搞定了。具体函数如下
/*判断是否双字节字符*/
public boolean isDoublebyteWord(String str){
byte[] b;
int temp;
for (int i = 0; i < str.length(); i++) {
b = str.substring(i, i + 1).getBytes();
temp = b[0];
if (temp > 0) {
return false;
}
}
return true;
}
/*给字符串添加换行符,其中linepos是需要换行的位置,按字节算的*/
public String lineStr(String s,int linePos){
String new_str="";
int total_len=0;
int brNum=0;
for(int i=1;i<=s.length();i++){
if(isDoublebyteWord(s.substring(i-1,i))){
total_len+=2;
if(total_len>=(linePos*(brNum+1))){
new_str+=s.substring(i-1,i)+"<br/>";
brNum++;
}else{
new_str+=s.substring(i-1,i);
}
}else{
total_len+=1;
if(total_len>=(linePos*(brNum+1))){
new_str+=s.substring(i-1,i)+"<br/>";
brNum++;
}else{
new_str+=s.substring(i-1,i);
}
}
}
return new_str;
}
在上一篇文章《将中英文混合的字符串换行》中提到了一个换行的算法,后来在实际应用中发现,还是有些问题的,比如,一行全是英文,或者全是中文,排出来的长度是不一样的。所以。后来不得不借用表格以及css来控制,其实也很简单,就加一句话 style="word-break:break-all"。这样自动换行没问题。
但是,我们还要实现的另外一个功能是:截取字符串的一部分,作为图片的简介,比如说:在一个iframe中显示最新的消息,配有图片,但是给出的位置不可能全部显示所有数据,因此要截取一部分。而且字符串中有 <br/>这样的换行符,因为不能简单的截取。再有就是显示的行数不能超过三行。
经过一番努力,将于写了个截取函数,还能将就这样。函数如下
public String getShortString(String source,int cutPos){
String shortStr = "";
int brNum=0;
String tmp="";
String tmp2 = "";
String tmpShort="";
int pos = 0;
int total_len = 0;
try{
pos = source.indexOf("<br/>");
System.out.println("1 pos = "+pos);
if(pos>0){
if(source.length()<=cutPos){
tmpShort = source;
}else{
//判断在截取位置是否有<br/>出现,所以往前往后各取4位,判断是否有<br/>出现
//为什么取四,那是因为<br/>长度是5,而要<br/>中一个字符出现在cutPos位置,
//取四最可能是cutPos位置是> 或者 < ;所以取四包括了所有可能出现情况
//1:当原字符串长度大于截取位置+<br/>的长度
if ( (source.length()-cutPos) >=4){
if(cutPos>=4){
tmp = source.substring(cutPos-4,cutPos+4);
}else{
tmp= source.substring(0,cutPos+4);
}
}else{
if(cutPos>=4){
tmp = source.substring(cutPos-4,source.length());
}else{
tmp= source.substring(0,source.length());
}
}
System.out.println("1 tmp = "+tmp);
int ipos = tmp.indexOf("<br/>");
if (ipos>0){
tmpShort = source.substring(0,cutPos-4)+tmp.substring(0,ipos)+"<br/>";
}else{
tmpShort = source.substring(0,cutPos);
}
}
System.out.println("1 tmpShort = "+tmpShort);
tmp = tmpShort;
tmp2 = tmpShort;
while(pos>0){
brNum+=1;
tmp = tmp2.substring(0,pos);
if(brNum>2){
tmp = tmp2.substring(0,pos);
System.out.println("tmp="+tmp);
//shortStr+=tmp;
pos = 0;
//tmp2 = tmp;
}else{
shortStr+=tmp+"<br/>";
tmp = tmp2.substring(pos+5);
System.out.println("tmp 2 ="+tmp);
pos = tmp.indexOf("<br/>");
System.out.println("pos="+pos);
tmp2 = tmp;
}
}
if (brNum==1){
System.out.println("1");
if(tmp.length()>cutPos/2){
shortStr+=tmp.substring(0,cutPos/2)+" ...";//当有一个<br/>时 后面再也没有的时候,第二行截取设定长度的一半。
}else{
shortStr+=tmp+" ...";
}
}else if(brNum==2){
System.out.println("2");
if(tmp.length()>cutPos/3){
shortStr+=tmp.substring(0,cutPos/3)+" ...";//当有二个<br/>时 后面再也没有的时候,第三行截取设定长度的1/3。
}else{
shortStr+=tmp+" ...";
}
}else if(brNum==3){
System.out.println("3");
if(tmp.length()>cutPos/4){
shortStr+=tmp.substring(0,cutPos/4)+" ...";//当有三个<br/>时 后面再也没有的时候,第三行截取设定长度的1/4
}else{
shortStr+=tmp+" ...";
}
//shortStr+=tmp+"<br/>"+" ...";
}
}else{
if(source.length()<=cutPos){
tmpShort = source;
}else{
tmpShort = source.substring(0,cutPos)+" ...";
}
/* if(source.length()>cutPos){
if(tmpShort.length()>cutPos){
shortStr = tmpShort.substring(0,cutPos)+" ...";
}else{
shortStr = tmpShort+" ...";
}
}else{
shortStr = tmpShort;
}*/
shortStr = tmpShort;
}
}catch(Exception e){
if(source.length()<=cutPos){
tmpShort = source;
}else{
tmpShort = source.substring(0,cutPos)+" ...";
}
e.printStackTrace();
shortStr= tmpShort;
}
return shortStr;
}
作者Blog:http://blog.csdn.net/kasam/