现在有一个标题的集合,List<string> titleList,我要把他显示在页面上,要求中英文标题的长度大致相同,但是一个完整的英文单词不能从中间截开,这次大家有没有好的思路啊

解决方案 »

  1.   

    大家给出的代码都有好多可取之处,感觉都比我写的好,但是我感觉在字符串长度 控制方面好像还是不太到位,还有,这些代码我都是测过的,有什么样的问题我也都回复了,我把我写的程序也发出来吧,给大家看看,我写的程序不太好,见笑了,
    public static String getSubStr(String str){
    return getSubStr(str,40);
    }
       public static String getSubStr(String str,int lengthNeeded){
       //if(lengthNeeded==0)return "请输入非零数字";
       
       int length=str.length();
       if(length<=lengthNeeded){return str;}
       else{
      
       String string=null;
       //首先判断是否是英文字符串。在这里我自己写了一个函数isEnglish,
       //思路是:如果一个串里面英文字母是其他字母的三倍,就算是英文串了,它的作用是是否要对给定的长度进行扩大,如果是英文就扩大,
       if(isEnglish(str)){
       //如果当成是英语字符串处理的话,将lengthNeeded扩大1.5倍
       lengthNeeded=2*lengthNeeded-(int)(lengthNeeded/3);
           if(length<=lengthNeeded)string=str;
           else{
            char ch=str.charAt(lengthNeeded);
         //   System.out.println("ch:"+ch);
          //ch是英语字符或者是'
            if(!(ch>='A'&&ch<='Z')&&!(ch>='a'&&ch<='z')&&ch!='\''){
            string=str.substring(0,lengthNeeded);
            }else{
            
            int next=getLengthBetweenPointAndNextSpace(str, length,ch,
         lengthNeeded) ;
            int previous=getLengthBetweenPointAndPreviousSpace(str, ch,
         lengthNeeded);
            if(isFirstWord(str,lengthNeeded)){
            string=str.substring(0,lengthNeeded+next);
            }else{
            if(next<=0){
            next=length-lengthNeeded;
            if(next>=previous)return str;
            else string=str.substring(0,lengthNeeded-previous);
            }
            if(next>=previous)string=str.substring(0, lengthNeeded-previous);
            else string=str.substring(0,lengthNeeded+next);
            }
            }
           }//以下的else表示非英文字符串时候
       }else{
       char ch=str.charAt(lengthNeeded);
    //    System.out.println("ch:"+ch);
       if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')||ch=='\''){
       int next=getLengthBetweenPointAndNextSpace(str, length,ch,
         lengthNeeded) ;
            int previous=getLengthBetweenPointAndPreviousSpace(str, ch,
         lengthNeeded);
            if(isFirstWord(str,lengthNeeded)){
            
            string=str.substring(0,lengthNeeded+next);
           
            }else{
            if(next>=previous)
            {string=str.substring(0, lengthNeeded-previous+1);
    //         System.out.println("lengthNeeded-previous:::::"+(lengthNeeded-previous));
            }
            else string=str.substring(0,lengthNeeded+next);
            
            }
       }else{
      
       string=str.substring(0,lengthNeeded)+ch+"";
       
       
       
    //    System.out.println(string);
       }
       }
    //    System.out.println("string:"+string+string.trim().length());
       
       
       if(string.length()==str.length())return string;
       else return string+"...";
       }
       
       }
       /*判断一个字符串是英文字符串,还是其它字符串,*/
       private static boolean isEnglish(String str){
       str=str.trim();
       char[] chs=str.toCharArray();
       int count=0;
       int num=0;
       for(int i=0;i<chs.length;i++){
       if((chs[i]>='A'&&chs[i]<='Z')||(chs[i]>='a'&&chs[i]<='z')){
       count++;
       }else num++;
       }
       
       if(num==0||count/num>=3) return true;
       else return false;
       }
       //得到该字符后面的第一个空格
       private static int getLengthBetweenPointAndNextSpace(String str, int len, char ch,
    int lengthNeeded) {
    int position = 0;
    char cha;
    for (int i = lengthNeeded + 1; i < len; i++) {
    cha = str.charAt(i);
    if((cha>='a'&&cha<='z')||(cha>='A'&&cha<='Z')||cha=='\'')continue;
    else{
    position = i;
    break;
    }
    // System.out.println("next str.charAt("+i+") :"+str.charAt(i));

    // System.out.println("postionNext:" + (position-lengthNeeded));


    if(position==0)return 0 ;
    else return position-lengthNeeded;//如果position=0,则表示是最后一个单词
    }
       //得到该字符前面的第一个空格
       private static int getLengthBetweenPointAndPreviousSpace(String str, char ch,
    int lengthNeeded) {
    int position = 0;
    char cha;
    if (lengthNeeded <= 1)
    return lengthNeeded;
    else {
    for (int i = lengthNeeded-1; i > 0; i--) {
    cha = str.charAt(i);
    if((cha>='a'&&cha<='z')||(cha>='A'&&cha<='Z')||cha=='\'')continue;
    else {
    position = i;
    break;
    }
    //   System.out.println("previous str.charAt("+i+") :"+str.charAt(i));
    }
    }
    // System.out.println("positionPrivious:" + (lengthNeeded-position));
    if(position==0)return 0;
    return  lengthNeeded-position;
    }
        //判断是不是第一个单词
    private static boolean isFirstWord(String str, int position) {
    for (int i = 1; i < position; i++) {
    if (str.charAt(i) == ' ')
    return false;
    }
    return true;
    }
       //判断是不是最后一个单词,这个方法,我好像没有用
    private static boolean isLastWord(String str, int position) {
    for (int i = position + 1; i <= str.length() - 2; i++) {
    if (str.charAt(i) == ' ')
    return false;
    }
    return true;
    }测试程序如下:
       public class Test {
      public static  void main(String[] args){
      String bb12=" UE打开乱码的问题,在前9205字符中加入中文注释可以解决此问题,或者使用在UE的【文件】菜单中的【转换】-&gt;【UNICODE/UTF-8 到 UTF-8(Unicode编辑)】进行转换。";
          String bb13="Cost-effectiveness, reducing costs by 90% over proprietary databases with features that ensure a product's COGS remain low throughout its lifecycle; ";
          for(int i=1;i<70;i++){
           System.out.println(TestStr.toLength(bb12,i)); 
           System.out.println("=========================I:"+i);
           System.out.println(TestStr.toLength(bb13,i)); 
           System.out.println();
           System.out.println();
           System.out.println();
           System.out.println();
          }
      }
    }
      

  2.   

    昨天已经有人用CSS帮你实现了 精确控制用代码来做 不能说做不到 但至少很难 
    这个放到表示层去做 确实是最合适的
      

  3.   

    CSS   text-overflow:ellipsis;
      

  4.   

    http://topic.csdn.net/u/20081215/11/b7320bc6-42c1-44c0-b70e-00b2c3f1a2a4.html
      

  5.   

    呵呵,如果前后差一个英文单词的距离,那还是可以接受的,我测试的结果也可以看一下,感觉两种标题的差距还是有点大
    =====================================================================UE...
    =========================I:0
    Cost...
    UE...
    =========================I:1
    Cost...
    UE打...
    =========================I:2
    Cost...
    UE打开...
    =========================I:3
    Cost-effectiveness...
    UE打开乱...
    =========================I:4
    Cost-effectiveness...
    UE打开乱码...
    =========================I:5
    Cost-effectiveness...
    UE打开乱码的...
    =========================I:6
    Cost-effectiveness...
    UE打开乱码的问...
    =========================I:7
    Cost-effectiveness...
    UE打开乱码的问题...
    =========================I:8
    Cost-effectiveness...
    UE打开乱码的问题,...
    =========================I:9
    Cost-effectiveness...
    UE打开乱码的问题,在...
    =========================I:10
    Cost-effectiveness...
    UE打开乱码的问题,在前...
    =========================I:11
    Cost-effectiveness,...
    UE打开乱码的问题,在前9...
    =========================I:12
    Cost-effectiveness,...
    UE打开乱码的问题,在前92...
    =========================I:13
    Cost-effectiveness,...
    UE打开乱码的问题,在前920...
    =========================I:14
    Cost-effectiveness, reducing...
    UE打开乱码的问题,在前9205...
    =========================I:15
    Cost-effectiveness, reducing...
    UE打开乱码的问题,在前9205字...
    =========================I:16
    Cost-effectiveness, reducing...
    UE打开乱码的问题,在前9205字符...
    =========================I:17
    Cost-effectiveness, reducing...
    UE打开乱码的问题,在前9205字符中...
    =========================I:18
    Cost-effectiveness, reducing...
    UE打开乱码的问题,在前9205字符中加...
    =========================I:19
    Cost-effectiveness, reducing costs...
    UE打开乱码的问题,在前9205字符中加入...
    =========================I:20
    Cost-effectiveness, reducing costs...
    UE打开乱码的问题,在前9205字符中加入中...
    =========================I:21
    Cost-effectiveness, reducing costs...
    UE打开乱码的问题,在前9205字符中加入中文...
    =========================I:22
    Cost-effectiveness, reducing costs by...
    UE打开乱码的问题,在前9205字符中加入中文注...
    =========================I:23
    Cost-effectiveness, reducing costs by 9...
    UE打开乱码的问题,在前9205字符中加入中文注释...
    =========================I:24
    Cost-effectiveness, reducing costs by 90...
    UE打开乱码的问题,在前9205字符中加入中文注释可...
    =========================I:25
    Cost-effectiveness, reducing costs by 90%...
    UE打开乱码的问题,在前9205字符中加入中文注释可以...
    =========================I:26
    Cost-effectiveness, reducing costs by 90% over...
    UE打开乱码的问题,在前9205字符中加入中文注释可以解...
    =========================I:27
    Cost-effectiveness, reducing costs by 90% over...
    UE打开乱码的问题,在前9205字符中加入中文注释可以解决...
    =========================I:28
    Cost-effectiveness, reducing costs by 90% over...
    UE打开乱码的问题,在前9205字符中加入中文注释可以解决此...
    =========================I:29
    Cost-effectiveness, reducing costs by 90% over...
    UE打开乱码的问题,在前9205字符中加入中文注释可以解决此问...
    =========================I:30
    Cost-effectiveness, reducing costs by 90% over...
    UE打开乱码的问题,在前9205字符中加入中文注释可以解决此问题...
    =========================I:31
    Cost-effectiveness, reducing costs by 90% over...
    UE打开乱码的问题,在前9205字符中加入中文注释可以解决此问题,...
    =========================I:32
    Cost-effectiveness, reducing costs by 90% over proprietary...
    UE打开乱码的问题,在前9205字符中加入中文注释可以解决此问题,或...
    =========================I:33
    Cost-effectiveness, reducing costs by 90% over proprietary...
    UE打开乱码的问题,在前9205字符中加入中文注释可以解决此问题,或者...
    =========================I:34
    Cost-effectiveness, reducing costs by 90% over proprietary...
    UE打开乱码的问题,在前9205字符中加入中文注释可以解决此问题,或者使...
    =========================I:35
    Cost-effectiveness, reducing costs by 90% over proprietary...
    UE打开乱码的问题,在前9205字符中加入中文注释可以解决此问题,或者使用...
    =========================I:36
    Cost-effectiveness, reducing costs by 90% over proprietary...
    UE打开乱码的问题,在前9205字符中加入中文注释可以解决此问题,或者使用在...
    =========================I:37
    Cost-effectiveness, reducing costs by 90% over proprietary...
    UE打开乱码的问题,在前9205字符中加入中文注释可以解决此问题,或者使用在UE...
    =========================I:38
    Cost-effectiveness, reducing costs by 90% over proprietary databases...
    UE打开乱码的问题,在前9205字符中加入中文注释可以解决此问题,或者使用在UE...
    =========================I:39
    Cost-effectiveness, reducing costs by 90% over proprietary databases...
    UE打开乱码的问题,在前9205字符中加入中文注释可以解决此问题,或者使用在UE的...
    =========================I:40
    Cost-effectiveness, reducing costs by 90% over proprietary databases...
    UE打开乱码的问题,在前9205字符中加入中文注释可以解决此问题,或者使用在UE的【...
    =========================I:41
    Cost-effectiveness, reducing costs by 90% over proprietary databases...
    UE打开乱码的问题,在前9205字符中加入中文注释可以解决此问题,或者使用在UE的【文...
    =========================I:42
    Cost-effectiveness, reducing costs by 90% over proprietary databases...
    UE打开乱码的问题,在前9205字符中加入中文注释可以解决此问题,或者使用在UE的【文件...
    =========================I:43
    Cost-effectiveness, reducing costs by 90% over proprietary databases with...
    UE打开乱码的问题,在前9205字符中加入中文注释可以解决此问题,或者使用在UE的【文件】...
    =========================I:44
    Cost-effectiveness, reducing costs by 90% over proprietary databases with...
    UE打开乱码的问题,在前9205字符中加入中文注释可以解决此问题,或者使用在UE的【文件】菜...
    =========================I:45
    Cost-effectiveness, reducing costs by 90% over proprietary databases with...
    UE打开乱码的问题,在前9205字符中加入中文注释可以解决此问题,或者使用在UE的【文件】菜单...
    =========================I:46
    Cost-effectiveness, reducing costs by 90% over proprietary databases with...
    UE打开乱码的问题,在前9205字符中加入中文注释可以解决此问题,或者使用在UE的【文件】菜单中...
    =========================I:47
    Cost-effectiveness, reducing costs by 90% over proprietary databases with features...
    UE打开乱码的问题,在前9205字符中加入中文注释可以解决此问题,或者使用在UE的【文件】菜单中的...
    =========================I:48
    Cost-effectiveness, reducing costs by 90% over proprietary databases with features...
    UE打开乱码的问题,在前9205字符中加入中文注释可以解决此问题,或者使用在UE的【文件】菜单中的【...
    =========================I:49
    Cost-effectiveness, reducing costs by 90% over proprietary databases with features...
    UE打开乱码的问题,在前9205字符中加入中文注释可以解决此问题,或者使用在UE的【文件】菜单中的【转...
    =========================I:50
    Cost-effectiveness, reducing costs by 90% over proprietary databases with features...
    UE打开乱码的问题,在前9205字符中加入中文注释可以解决此问题,或者使用在UE的【文件】菜单中的【转换...
    =========================I:51
    Cost-effectiveness, reducing costs by 90% over proprietary databases with features that...
    UE打开乱码的问题,在前9205字符中加入中文注释可以解决此问题,或者使用在UE的【文件】菜单中的【转换】...
    =========================I:52
    Cost-effectiveness, reducing costs by 90% over proprietary databases with features that...
    UE打开乱码的问题,在前9205字符中加入中文注释可以解决此问题,或者使用在UE的【文件】菜单中的【转换】-...
    =========================I:53
    Cost-effectiveness, reducing costs by 90% over proprietary databases with features that...
    UE打开乱码的问题,在前9205字符中加入中文注释可以解决此问题,或者使用在UE的【文件】菜单中的【转换】-&...
    =========================I:54
    Cost-effectiveness, reducing costs by 90% over proprietary databases with features that...
      
      

  6.   

    那楼主把你的接口方法精确说一下吧
    你在第一个帖子里发的是
    SubStr(String title,int lengthAssigned); 
    但是返回值不知道啊
      

  7.   

    哦,呵呵,对不起啊,
       public static String SubStr(String title,int lengthAssigned);
    参数的意思:title代表标题,可以是任意的字符串
                lengthAssigned代表用户指定的长度
    另外我还想:能不能用一个list把所有的标题给传进这个函数,然后通过比较所有标题的长度,以及用户制定的长度,来得到要显示的字符串。接口如下:
    public  static List<String> subStr(List<String> titleList,int lengthAssigned);
      

  8.   

    嗯,简单写了一下。
    仅供参考public class Test6 { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    TestSubString ts = new TestSubString();
    //String chn_title = "秋のサケの網漁は日本の最北の北海道でずっと太平洋岸を始めています";
    String chn_title="祝大家新年愉快祝大家新年愉快";
    //String eng_title = "Autumn salmon net fishing has begun along the Pacific coast in Japan's northernmost prefecture of Hokkaido";
    String eng_title = "Everybody Happy new yearEverybody Happy new year";
    String[] res = ts.getTitle(chn_title, eng_title, 10);
    for(String s: res){
    System.out.println(s);
    }
    }}interface SubString{        //传入中文标题,英文标题和长度,返回一个包含截取后的中,英文标题的数组
    String[] getTitle(String chn_contents, String eng_contents, int len);
    }class TestSubString implements SubString{
    public String[] getTitle(String chn_title, String eng_title, int len){
    String[] result = new String[2];

    String chn = chn_title.substring(0,len);
    int eng_len = len/2+len;    //英文标题的长度大概是中文的1.5倍长
    while(true){
    char temp = eng_title.charAt(eng_len);
    if(temp!=0x20){      //是否截取在单词的中间,0x20是空格
    eng_len++;
    }else{
    break;
    }
    }

    String eng = eng_title.substring(0,eng_len);

    result[0] = chn;
    result[1] = eng;

    return result;
    }
    }