编程:编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个”。

解决方案 »

  1.   

    public class test{  
        public void splitIt(String splitStr, int bytes) {  
        int cutLength = 0;  
        int byteNum = bytes;  
        byte bt[] = splitStr.getBytes();  
        System.out.println("Length of this String ===>" + bt.length);  
        if (bytes > 1) {  
        for (int i = 0; i < byteNum; i++) {  
        if (bt[i] < 0) {  
        cutLength++;      }  
        }      if (cutLength % 2 == 0) {  
        cutLength /= 2;  
        }else  
        {  
        cutLength=0;  
        }  
        }  
        int result=cutLength+--byteNum;  
        if(result>bytes)  
        {  
        result=bytes;  
        }  
        if (bytes == 1) {  
        if (bt[0] < 0) {  
        result+=2;      }else  
        {  
        result+=1;  
        }  
        }  
        String substrx = new String(bt, 0, result);  
        System.out.println(substrx);      }      public static void main(String args[]) {  
        String str = "我abc的DEFe呀fgsdfg大撒旦";  
        int num =3;  
        System.out.println("num:" + num);  
        test sptstr = new test();  
        sptstr.splitIt(str, num);  
        }      } 
    运行情况:
    num:3 
    Length of this String ===>25 
    我a num:2 
    Length of this String ===>25 
    我 num:1 
    Length of this String ===>25 
    我 num:4 
    Length of this String ===>25 
    我ab
      

  2.   

    public class test {
    public static void main(String[] args) {
    String s = "我ABC汉DEF";
    System.out.println(sub(s, 6));
    } public static String sub(String str, int num) {
    if (num <= 0)
    return "";
    StringBuffer sb = new StringBuffer();
    char[] ch = str.toCharArray();
    for (int count = 0, i = 0; i < ch.length; i++) {
    char c = ch[i];
    do {
    count++;
    } while ((c >>= 8) != 0);
    if (count > num)
    break;
    sb.append(ch[i]);
    }
    return sb.toString();
    }
    }结果:
    我ABC
      

  3.   

    class SplitString {        String SplitStr;    int SplitByte;    public SplitString(String str, int bytes) {
            SplitStr = str;
            SplitByte = bytes;
            System.out.println("The String is:′" + SplitStr + "′;SplitBytes="
            + SplitByte);
        }    public static void main(String[] args) {
            SplitString ss = new SplitString(
                    "test中dd文fasjaslkjdfalsjf师的看见发lkjd",4);
            ss.split();
        }    public void split() {
            for (int i = 0, len = SplitStr.length(); i < len; i = i + SplitByte) {
                if (i + SplitByte < len) {
                    System.out.println(SplitStr.substring(i, i + SplitByte));
                }
                else {                System.out.println(SplitStr.substring(i, len));
                }
            }
        }
    }
      

  4.   

    根据unicode编码,判断出哪些是汉字就可以了
      

  5.   

    混个分~时刻警醒不能眼高手低~~    public static void main(String[] args) throws IOException {
            int cnt = 7;
            BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
            char[] c = stdIn.readLine().toCharArray();
            int already = 0;//计数器.汉字+2其它+1
            for (int i = 0; i < cnt; i++) {
                if (i >= c.length || already > cnt)
                    break;
                char cc = c[i];
                if ((cc >>= 8) == 0)
                    already += 2;
                else
                    already += 1;
                System.out.print(c[i]);
            }
        }
      

  6.   


    其实用不着while的应该,char最大也就16位吧应该……
    >255我想过,不过我想万一高位依然是负的……那就不妙了……
      

  7.   


    这个题啊,如果用C语言处理还有些道理,用java处理有毛意思?java中char类型是16位的,可以容纳一个汉字了。
      

  8.   

    package test; 
    class SplitString 

    String SplitStr; 
    int SplitByte; 
    public SplitString(String str,int bytes) 

    SplitStr=str; 
    SplitByte=bytes; 
    System.out.println("The String is:´"+SplitStr+"´;SplitBytes="+SplitByte); 

    public void SplitIt() 

    int loopCount; 
    loopCount=(SplitStr.length()%SplitByte==0)?(SplitStr.length()/SplitByte):(SplitStr.length()/Split Byte+1); 
    System.out.println("Will Split into "+loopCount); 
    for (int i=1;i<=loopCount ;i++ ) 

    if (i==loopCount){ 
    System.out.println(SplitStr.substring((i-1)*SplitByte,SplitStr.length())); 
    } else { 
    System.out.println(SplitStr.substring((i-1)*SplitByte,(i*SplitByte))); 



    public static void main(String[] args) 

    SplitString ss = new SplitString("test中dd文dsaf中男大3443n中国43中国人 0ewldfls=103",4); 
    ss.SplitIt(); 

    }刚回完一个,能看懂不?
      

  9.   

    public class SplitString {
    public static void split(String source, int num) throws Exception {
    int k = 0;
    String temp = "";
    for (int i = 0; i < source.length(); i++) {
    byte[] b = (source.charAt(i) + "").getBytes();
    k = k + b.length;
    if (k > num) {
    break;
    }
    temp = temp + source.charAt(i);
    }
    System.out.println(temp);
    }
    public static void main(String[] args) {
    SplitString ss = new SplitString();
    try{ss.split("我ABC汉DEF",6);}
    catch(Exception e){}
    finally{}
    }
    }对不起,没弄好,自己又写了一个,希望对你有帮助
      

  10.   

    我ABC汉DEF我们不能直接使用String类的substring(int beginIndex, int endIndex)方法,因为它是按字符截取的。'我'和'A'都被作为一个字符来看待,length都是1。实际上我们只要能区分开中文汉字和英文字母,这个问题就迎刃而解了,而它们的区别就是,中文汉字是两个字节,英文字母是一个字节。 
    Java代码 
    public class CutString { /** 
    * 判定是否是一个中文汉字 

    * @param c 
    * 字符 
    * @return true表示是中文汉字,false表示是英文字母 
    */ 
    public static boolean isChineseChar(char c) { 
    // 假如字节数大于1,是汉字 
    return String.valueOf(c).getBytes().length > 1; 
    } /** 
    * 按字节截取字符串 

    * @param orignal 
    * 原始字符串 
    * @param count 
    * 截取位数 
    * @return 截取后的字符串 
    */ 
    public static String substring(String orignal, int count) { 
    // 原始字符不为null,也不是空字符串 
    if (orignal != null && !"".equals(orignal)) { 
    // 要截取的字节数大于0,且小于原始字符串的字节数 
    if (count > 0 && count < orignal.getBytes().length) { 
    StringBuffer buff = new StringBuffer(); 
    char c; 
    for (int i = 0; i < count; i ) { 
    c = orignal.charAt(i); 
    buff.append(c); 
    if (CutString.isChineseChar(c)) { 
    // 碰到中文汉字,截取字节总数减1 
    --count; 


    return buff.toString(); 


    return orignal; 
    } public static void main(String[] args) { 
    // 原始字符串 
    String s = "我ABC汉DEF"; 
    System.out.println("原始字符串:" s); 
    System.out.println("截取前1位:" CutString.substring(s, 1)); 
    System.out.println("截取前2位:" CutString.substring(s, 2)); 
    System.out.println("截取前4位:" CutString.substring(s, 4)); 
    System.out.println("截取前6位:" CutString.substring(s, 6)); 

    } 运行结果: 1、原始字符串:我ABC汉DEF
    2、截取前1位:我 
    3、截取前2位:我 
    4、截取前4位:我AB 
    5、截取前6位:我AB汉
      

  11.   

    我想没大家想象的那么复杂吧?只需要对截取的最后一个字节进行一个控制就可以了啊,如果是一个汉字的第一个字节,那么就不要,或者是往后再截取一位,就ok了啊!下面是我写的一个demo,仅供参考,恶哈哈!
    public class StringSpiltByByte { @SuppressWarnings("unused")
    private String subString(String str, int len) { byte[] b = str.getBytes(); if (len == 1) {// 当只取1位时
    if (b[0] < 0)
    return new String(b, 0, 2);
    else
    return new String(b, 0, len);
    } else { if (b[len - 1] < 0 && b[len - 2] > 0) { // 判断最后一个字节是否为一个汉字的第一个字节 return new String(b, 0, len - 1);
    }
    }
    return new String(b, 0, len);
    } public static void main(String[] args) {
    String str = "我ABC汉DEF";
    System.out.println(new StringSpiltByByte().subString(str, 1)); }
    }