哪有你这么读的阿!当然如果你非要这么读的话,先把编码变为UTF-8就可以了前面3个字母每个字母一个字节!

解决方案 »

  1.   

    用unicode编码,读6个字节
    明明只读了“abc”三个字母阿!
      

  2.   

    用getBytes()转换成字节后,取前面四字节再转换成String
      

  3.   

    好变态的需求阿public class Test {
        public static void main(String[] args){
    String test = "abc中国";
    byte[] byteArray = test.getBytes();
    // String newTest = new String(byteArray, 0, 4); 没有任何输出
                      // String newTest = new String(byteArray, 0, 3); 输出 abc
                      String newTest = new String(byteArray, 0, 5); 输出 abc中
    System.out.println(newTest);
        }
    }
      

  4.   

    要将字串转成何种编码
    输出6字节才会是“abc中”
    好象都不行吗
      

  5.   

    package change;
    //import java.util.*;
    //import java.text.*;
    import java.io.*;
    public class InterceptChString
    {
    public String subChString(String msg, int nOut){ byte ch[]=msg.getBytes();
    if(ch.length<=nOut) return msg;
    int nLen = 0;
    while(nLen <= ch.length && nLen<=nOut){ 
    if ((msg.substring(0,nLen).getBytes().length - nOut) >=0)
    break;
    nLen++;
    }
    return(msg.substring(0,nLen));
    }
    }
      

  6.   

    public class Test {
    public static void main(String[] args) {
    String s = "abc中国";
    byte[] b = s.getBytes();
    StringBuffer buf = new StringBuffer(4);
    for(int i=0;i<4;i++) {
    char c = (char)b[i];
    buf.append(c);
    }
    System.out.println(buf);
    }
    }
    效果不好 :-(,据说很多可以删除半个汉字的编辑软件之所以出现乱码是因为和前面或者后面的字符进行了整合。
      

  7.   

    用unicode编码,读8个字节