Chinaunix首页 | 论坛 | 博客
  • 博客访问: 26277511
  • 博文数量: 2065
  • 博客积分: 10377
  • 博客等级: 上将
  • 技术积分: 21525
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-04 17:50
文章分类

全部博文(2065)

文章存档

2012年(2)

2011年(19)

2010年(1160)

2009年(969)

2008年(153)

分类: Java

2009-05-13 19:07:41

1.截取一个长的字符串按照指定的长度进行切分出来
     /**
     * 截取长字符串的某部分进行显示出来
     * */
        public static int byteLength(String string){       
            int count = 0;
            for(int i=0;i            {
                if(Integer.toHexString(string.charAt(i)).length() == 4){
                    count += 2;
                }else{
                    count++;
                }
            }
            return count;
        }
        /**
         * 按指定长度,省略字符串部分字符
            @para String 字符串
            @para length 保留字符串长度
            @return 省略后的字符串
         * */
        public static String omitString(String string,int length){
            StringBuffer sb = new StringBuffer();
            if(byteLength(string)>length){
                int count = 0;
                for(int i=0;i                    char temp = string.charAt(i);
                    if(Integer.toHexString(temp).length()==4){
                        count += 2;
                    }else{
                        count++;
                    }if(count                        sb.append(temp);
                    }
            if(count==length-3){
                sb.append(temp);
                break;
            }
            if(count>length-3){
                sb.append(" ");
                break;
            }
                }
            sb.append("...");
            }else{
            sb.append(string);}
            return sb.toString();
        }
这样的话方便截取长字符串。

阅读(1028) | 评论(0) | 转发(0) |
0

上一篇:JAVA连接各个DB整理

下一篇:说明

给主人留下些什么吧!~~