//测试类public class Testsplit { public static void main(String[] args) 
{
String str1=split79L.left("中abc国a",4);
System.out.println(str1);
String str2=split79L.left("中abc国a",6);
System.out.println(str2);
    }
}//引用 10 楼 blliy117 的回复:(有稍许改动)class splitstr10L
{

    public static String left(String str, int end) 
    {
        if(str.length()*2 <end)
        {
            return str;
        }
        
        char[] chArr = str.toCharArray();
        
        int lenByte = 0;
        for (int i = 0; i < chArr.length; i++) 
        {
            if (chArr[i] > 255)
            {
                lenByte += 2;
            } else 
            {
                lenByte++;
            }
            
        if(lenByte==end)
            return str.substring(0,i+1);//原return str.substring(0,i);
        
        if(lenByte>end)
         return str.substring(0,i);//原return str.substring(0,i-1); 
        }
        return str;
    }  
}//#15楼
public class split15L {
public static String left(String str,int n){
        String restr="";
        int tmp=0;
        for(int i=0;i<n;i++){
            char c=str.charAt(i);
            if(String.valueOf(c).getBytes().length==2){    
                tmp++;
            }else{
            }
            if(i+tmp>=n){
                break;
            }
            restr+=c;
        }
        return restr;
    }
}//#45楼
public class split45L {
public static String left(String string, int bytes){
        char[] words = string.toCharArray();
        String result = new String();
        for(int i=0; i<words.length; i++){
            char word = words[i];
            String s = word + "";
            result = result + s;
            if(result.getBytes().length == bytes){
                break;
            }else if(result.getBytes().length > bytes){
                result = result.substring(0, result.length()-1);
                break;
            }
        }
        
        return result;
    }
}//引用 60 楼 fanjin287659245 的回复:public class splitstr60L { public static String left(String str, int n) { if (n > str.length())
n = str.length(); byte[] bt = null;
int count = 0;
String ch;
StringBuffer strbuf = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
ch = str.substring(i, i + 1);
bt = ch.getBytes();
count += bt.length;
if (count <= n) {
strbuf.append(ch);
} else {
return strbuf.toString();
}
}
return strbuf.toString();
}
}//#67楼
public class split67L {
public static String left(String str, int index){
        char[] c = str.toCharArray();
        int in=0;
        for(int i=0;i<c.length;i++)
        {
            if(c[i]>255)
            {
                in+=2;
            }else
            {
                in++;
            }
            if(in==index)
            {
                return(str.substring(0,i+1));
            }else if(in>index)
            {
                return(str.substring(0,i));
            }
        }
        return null;
    }
}
//#79楼public static String left(String str, int len) {
        if(len==0)return "";
        
        char[] chArr = str.toCharArray();
        int n=0;
        int lenByte = 0;
        
        for (int i = 0; i < chArr.length; i++) {
            lenByte+=(chArr[i]+"").getBytes().length;
            if(lenByte>len)
            { 
                n=i;
                break;                    
            }                 
       }
        return str.substring(0,n);
    }//#162楼 得分:0回复于:2009-08-08 13:10:38
public class split162L {
public static String left(String str, int n) {
String ret = "";
char[] array = str.toCharArray();
for (int i = 0, j = 0; i < array.length && j < n; i++) {
j += String.valueOf(array[i]).getBytes().length;
if (j > n) {
break;
}
ret += array[i];
}
return ret;
}
}
小程序,很有意思。于是便整理一下,让大家看的方便一些。ps:
不要再用“学习了”、“看一下”、“顶一下”之类的话了。你还是个无聊的小孩吗?
你知道,这一句一句的废话,给别人的阅览带来多大的不便……

解决方案 »

  1.   

    from:http://topic.csdn.net/u/20090805/19/8f0b4b36-d4c7-47b0-8570-dff46a2c84f4.html
      

  2.   

    看来楼主还是没找到很好的答案:
    public class CutByBytes {    public static void main(String[] args) {
            String str = "123中华人民共和国";
            System.out.println(left(str, 0));   // ""
            System.out.println(left(str, 1));   // "1"
            System.out.println(left(str, 5));   // "123中"
            System.out.println(left(str, 6));   // "123中"
        }    private static String left(String str, int byte_len) {
            if (str.getBytes().length < byte_len) {
                return str;
            }        int len = byte_len;
            String substr = str.substring(0, len);
            while (substr.getBytes().length > byte_len) {
                len--;
                substr = str.substring(0, len);
            }
            return substr;
        }
    }
      

  3.   

    之前贴的代码加一点更正:
    public class CutByBytes {    public static void main(String[] args) {
            String str = "123中华人民共和国";
            System.out.println(left(str, 0));   // ""
            System.out.println(left(str, 1));   // "1"
            System.out.println(left(str, 5));   // "123中"
            System.out.println(left(str, 6));   // "123中"
        }    private static String left(String str, int byte_len) {
            if (str.getBytes().length < byte_len) {
                return str;
            }        int len = str.length();
            String substr = str.substring(0, len);
            while (substr.getBytes().length > byte_len) {
                len--;
                substr = str.substring(0, len);
            }
            return substr;
        }
    }
      

  4.   

     int len = str.length();
            String substr = str.substring(0, len)这样是有啥用意?
      

  5.   

    http://blog.csdn.net/YidingHe/archive/2009/08/08/4426369.aspx
      

  6.   


    int len = str.length();   
    String substr = str.substring(0, len);   //substr为何不直接赋值呢?
    //你那样写不就相当于String substr=str;吗?
      

  7.   

    这个题目或者是类似题目确实很惹眼啊。
    我记得半年前去某公司面试,考了我这个题目,
    然后上个月去面试个J2ME的工作,也考了类似题目。
    还好都写上了。
    不过面J2ME的面试官一再让我优化代码,
    搞我的很纠结啊。LZ加油。
      

  8.   

    尽管写的很菜,但也还是贴出来,在看3楼的回帖以前,我还真不知道byte数组也有length方法..失败..
    public class Demo {

    /**
     * Method main
     *
     *
     * @param args
     *
     */
    private String left(String str,int n)
    {
    byte [] buf=str.getBytes();
    String mystr="";

    for(int i=0;i<buf.length;)
    {
    if(buf[i]>0)
    {
    mystr=mystr+"0";
    i++;
    }
    else
    {
    mystr=mystr+"12";
    i++;
    i++;
    } if(n>mystr.length())
    {
    return str;
    }

    if(mystr.substring(n-1,n).equals("1"))
    {
    n--;
    //System.out.println (n);
    }
    else
    {
    }
    }
    return new String(buf,0,n);
    }
    public static void main(String[] args) 
    {
    // TODO: Add your code here
    String str="中abc国11";
    byte [] buf=str.getBytes();
    Demo d=new Demo();
    System.out.println (d.left(str,24));
    }
    }测试了一下,能满足楼主所需的功能.