我现在要将一系列的中文和英文的标题展示到页面的标题列表中,要求中英文标题的长度大致相等,并且英文不允许从一个单词中间截开,
   比如:如果标题为如下两个:
     (1)如果一个人轻轻走过,你是否被他的清风带动
     (2)if a man came by with you  lightly, whether you would be reflected  by him
    如果我指定长度为10,则第一个标题为:
            如果一个人轻轻走过...
     第二个为:
            if a man ...(由于c在come中前半部分位置,故不要整个单词)   
此时两个标题的长度的差别就很明显,那么我如何能将两个标题的长度条的差不多呢,当然可以修改第二个的长度,但是必须得以给定的长度为标准。接口方法  SubStr(String title,int lengthAssigned);    
               
在线等,急用,

解决方案 »

  1.   

    可以先按空格切割整个String 然后计算的时候再不会来就好了 
      

  2.   


    public static String subString(String str, int sub) {
    if (str != null) {
    Pattern pattern = Pattern.compile("[\u4e00-\u9fa5]|[\uFF00-\uFFEF]", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
    Matcher m = pattern.matcher(str);
    if (m.find()) {
    //中文
    if (str.length() <= sub) {
    return str;
    }
    System.out.print("中文: ");
    str = str.substring(0, sub) + "...";
    return str;
    } else {
    System.out.print("英文: ");
    sub *= 2;
    if (str.length() <= sub) {
    return str;
    }
    str = str.substring(0, sub);
    str = str.substring(0, str.lastIndexOf(" ")) + "...";
    return str;
    }
    }
    return null;
    }
    public static void main(String[] args) {
    String str = "如果一个人轻轻走过,你是否被他的清风带动";
    System.out.println(subString(str, 10));
    str = "if a man came by with you  lightly, whether you would be reflected  by him ";
    System.out.println(subString(str, 10));
    }
    中文: 如果一个人轻轻走过,...
    英文: if a man came by...
      

  3.   

    public class Test{ 
    //public static Timer t=new Timer();
        public static void main(String args[]) throws Exception {
         String[] titles={"如果一个人轻轻走过,你是否被他的清风带动",
                      "if a man came by with you  lightly, whether you would be reflected  by him "
                      };
         for(String title : titles){
         System.out.println(SubStr(title,10));
         }
        } public static String SubStr(String title,int lengthAssigned){
         int leng=2*lengthAssigned;
         int index=0,totalLeng=0;
         boolean hasChinese=false;
         while(index<title.length()){
         if(title.charAt(index)<256){
         totalLeng++;
         }else{
         totalLeng+=2;
         hasChinese=true;
         }
         if(totalLeng-leng>=0){
         break;
         }
         index++;
         }
         if(hasChinese){
         return title.substring(0,index);
         }
         int indexOfSpace=title.lastIndexOf(" ",index);
         return title.substring(0,indexOfSpace);
        
        }
     }
    F:\java>java Test
    如果一个人轻轻走过
    if a man came by
      

  4.   

    public class TestString { public static void main(String[] args) {

    TestString ts = new TestString();
    String s = "if a man came by with you  lightly, whether you would be reflected  by him";
    System.out.println(ts.SubStr(s, 20));
    }

    String SubStr(String title, int lengthAssigned){  //英文
    int len = title.length();
    if(len <= lengthAssigned){
    return title;
    }
    String temp = title.substring(0, lengthAssigned);
    if(" ".equals(title.substring(lengthAssigned, lengthAssigned + 1))){
    return temp + "...";
    }
    int sawp = temp.lastIndexOf(" ");
    return temp.substring(0,sawp) + "...";
    }
    }只做了英文的....
      

  5.   

    这种标示层的东西放在css里处理最合适不过了
    <div style="font-size: 12pt;">
    <span   style="width:180; height:18;  overflow   :   hidden; word-break   :   keep-all">   
      如果一个人轻轻走过,你是否被他的清风带动 </span>...</div>
    <div style="font-size: 12pt;">
    <span   style="width:180; height:18;  overflow   :   hidden; word-break   :   keep-all">   
      if a man came by with you  lightly, whether you would be reflected  by him </span>...</div>
      

  6.   


    程序有异常!!!!!!!!!!!!!!!!!!!!!!!
    =========================sub:1
    英文: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.substring(String.java:1938)
    at com.isiteam.TestStr.toLength(TestStr.java:26)
    at com.isiteam.Test.main(Test.java:10)
      

  7.   


    你设置的长度是多少啊,我用另外两个字符串测了以下,youyichang
     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; ";
         
      

  8.   


    public static String subString(String str, int sub) {
    if (str != null) {
    Pattern pattern = Pattern.compile("[\u4e00-\u9fa5]|[\uFF00-\uFFEF]", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
    Matcher m = pattern.matcher(str);
    if (m.find()) {
    //中文
    if (str.length() <= sub) {
    return str;
    }
    System.out.print("中文: ");
    str = str.substring(0, sub) + "...";
    return str;
    } else {
    System.out.print("英文: ");
    sub *= 2;
    if (str.length() <= sub) {
    return str;
    }
    String str1 = str.substring(0, sub);
    String str2 = str.substring(sub, str.length());
    int a = str1.substring(str1.lastIndexOf(" "), str1.length()).length();
    int b = str2.substring(0, str2.indexOf(" ")).length();
    str =  a >= b ? str.substring(0, sub + b) : str.substring(0, sub - a);
    return str + "...";
    }
    }
    return null;
    }
    public static void main(String[] args) {
    String str = "如果一个人轻轻走过,你是否被他的清风带动";
    System.out.println(subString(str, 10));
    str = "if a man came by with you  lightly, whether you would be reflected  by him ";
    System.out.println(subString(str, 10));
    }这个是加了缩进的
      

  9.   


    异常如下:
    中文:  ...
    =========================I:1
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.substring(String.java:1932)
    at com.isiteam.TestStr.toLength(TestStr.java:27)
    at com.isiteam.Test.main(Test.java:10)
    英文: 
      

  10.   

    F:\java>java Test
    如果一个人轻轻走过
    if a man came by
    UE打开乱码的问题,
    Cost-effectiveness,
      

  11.   

    加一些注释,做一些调整。    //思路是长度以汉字为准,一个汉字按2算,1个半角字符按1算,都向汉字看起。
        //考虑到中英文混用的情况,所以一个字符一个字符拿出来看
        //参数lengthAssigned被看做是汉字的个数。
        //用totalLeng来记数。 public static String SubStr(String title,int lengthAssigned){

         int leng=2*lengthAssigned;
         int index=0,totalLeng=0;
         boolean hasChinese=false;
         while(index<title.length()){
         if(title.charAt(index)<256){
         totalLeng++;
         }else{
         totalLeng+=2;
         hasChinese=true;
         }
         if(totalLeng-leng>=0){
         break;
         }
         index++;
         }
         if(hasChinese){          //如果前面的字符中包含有全角字符,则直接截取字符串。
         return index==0?"":title.substring(0,index+1);
         }
         //在规定长度之内全是半角字符时的处理:
         //
        
         if(index==0){
          return "";
         }
         int indexOfSpace=title.lastIndexOf(" ",index+1);
         if(indexOfSpace<0){
         return title.substring(0,index+1);
         }
         return title.substring(0,indexOfSpace+1);
        
        }
      

  12.   

    刚刚自己做的,应该可以!!import java.util.StringTokenizer;
    public class equalLengthString {

    public static String cutString(String s, int length){

    if(s.length() < length){//小于截取长度返回全部
    return s;
    }

    if(s.getBytes().length > s.length()){//是中文字符

    return s.substring(0, length-1);

    }
    //英文字符
    StringTokenizer st = new StringTokenizer(s," ");

    String str = "";

    while (st.hasMoreElements()) {

    String s1= (String) st.nextElement()+" ";

    if(str.length() + s1.length() - 1 > length){//排除末尾“ ”
    break;
    }
    str += s1;
    }

    return str;

    }

    public static void main(String[] args) {
    String str = "如果一个人轻轻走过,你是否被他的清风带动 ";
    String str1 = "if a man came by with you  lightly, whether you would be reflected  by him"; 

    System.out.println(equalLengthString.cutString(str, 10));
    System.out.println(equalLengthString.cutString(str1, 10));


    }}
      

  13.   

    if(str.length() + s1.length() - 1 > length){//排除末尾“ ” 
    break; 

    这一行如果是英文的话,无法解析,再说了,str你也没有赋值啊
      

  14.   

    str是在if后面被赋值的,对吧,呵呵,你拿这个字符串测以下吧,看看效果如何
    :Cost-effectiveness, reducing costs by 90% over proprietary databases with features that ensure a product's COGS remain low throughout its lifecycle; 
      

  15.   

    哥们 ,你的程序只能进入到中文:
    这是我的测试代码:
      public static void main(String[] args) { 
    String str = "UE打开乱码的问题,在前9205字符中加入中文注释可以解决此问题,或者使用在UE的【文件】菜单中的【转换】-&gt;【UNICODE/UTF-8 到 UTF-8(Unicode编辑)】进行转换"; 
    String str1 = "Cost-effectiveness, reducing costs by 90% over proprietary databases with features that ensure a product's COGS remain low throughout its lifecycle; "; // System.out.println(subString(str, 10)); 
    // System.out.println(subString(str1, 10)); 
                  for(int i=0;i<50;i++){
             System.out.println(subString(str, i)); 
         System.out.println(subString(str1, i)); 
            }