■式样要求如下
  要求从左向右取5个文字(全角,半角都可能包含)。
  假如:
   String  s1= "测试数据从左向右取得";
   String  s2= "0测试数据从左向右取得";
   String  s3= "0123456789";  结果:
   S1的场合:"测试"
   S2的场合:"0测试"
   S3的场合:"01234"   注意:S1的场合,因为"数"占有2个字节,不能将一个字从中间断开,所以只取前面的数据。■实现方式
   我用了getBytes()等方法,搞了一天也没实现。请高手帮忙阿,最好能写个通过的方法。

解决方案 »

  1.   

    为什么转换成byte,转换成char型的不行吗
      

  2.   

    一个char占2个字节,你截5个字节
      

  3.   

    用这两个方法试一下吧substring(arg0, arg1)或subSequence(beginIndex, endIndex)
      

  4.   

    5个字节的文件
      那你还需要转啦
    怎么这么个需求。楼主还是讲明白点。
    String 一般截取字符串  用subString(),就行了。
      

  5.   

    Java中字符和汉字都占2字节,是说在编译器编译成Class 文件的时候使用的Unicode 编码
    getBytes()  是使用平台默认的字符集,汉字2字节,字母和字符1字节。public class Test {
    public static void main(String[] args) {int beginFlag = 0;
    int endFlag = 5;
    StringBuffer outStr = new StringBuffer();
    String codingStr = "java程序";
    for (Character i : codingStr.toCharArray()) {
    int length = i.toString().getBytes().length;
    if (beginFlag < endFlag) {
    beginFlag += length;
    outStr.append(i.toString());
    }
    }
    System.out.println(outStr.toString());
    }
    }
    跑一下就知道了
      

  6.   

    不好意思,是取5个字节。日文系统中,全角占用2个字节,半角占用1个字节。如果有一个字段,DB中定义的长度为varchar2(100)。画面需要显示的是:此字段的前20字节(可能包含全角和 半角)。如果遇到第20个字节刚好是全角字符的话,则不显示最后一个全角。如上面的例子所示、*我尝试了很多,不是那么容易实现的。
      

  7.   

    subString方法是取得个数。getByte是取字节
      

  8.   

    是substring不是subString吧……
    不可以一个字符一个字符的录,判断全半然后用counter累加吗?
    我是新手,刚学一个星期,54我……
      

  9.   

    自己写了一个基本的,权当抛砖引玉,不是很完善的。import java.io.IOException;
    import java.nio.ByteBuffer;
    import java.nio.CharBuffer;
    import java.nio.charset.Charset;
    import java.nio.charset.CharsetDecoder;
    import java.util.Arrays;public class Decoder { public static String decode(byte[] b, String charsetName) {
    ByteBuffer in = ByteBuffer.wrap(b);
    Charset charset = Charset.forName(charsetName);
    CharsetDecoder decoder = charset.newDecoder();
    CharBuffer out = CharBuffer.allocate(b.length);
    out.clear();
    decoder.decode(in, out, false);
    out.flip();
    return out.toString(); } public static void main(String[] args) throws IOException {
    String s = "abc测c试字符串试试看";
    byte[] b = s.getBytes("UTF-8");
    byte[] r = Arrays.copyOf(b, 9);
    System.out.println(decode(r, "UTF-8")); }}
      

  10.   

    public class LeftByBytes {
    public static String leftByBytes(String input, int length, String encoding){
    if (input==null) return null;
    if (length<=0) return "";
    StringBuffer result= new StringBuffer("");
    int byteLength = 0;
    int index = 0;
    while (index < input.length()){
    String current = input.substring(index, index+1);
    int wide;
    try {
    wide = current.getBytes(encoding).length;
    } catch (UnsupportedEncodingException e) {
    wide = current.getBytes().length;
    }
    if (byteLength + wide > length) break;
    result.append(current);
    byteLength += wide;
    index ++;
    }
    return result.toString();
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
      String s1= "测试数据从左向右取得";
      String s2= "0测试数据从左向右取得";
      String s3= "0123456789";
      String s4= "短";
      System.out.println(leftByBytes(s1, 5, "GBK"));
      System.out.println(leftByBytes(s2, 5, "GBK"));
      System.out.println(leftByBytes(s3, 5, "GBK"));
      System.out.println(leftByBytes(s4, 5, "GBK"));
    }}
      

  11.   

        public static void main(String[] args) {
            try {
                String[] srcArray = new String[]{"测试数据从左向右取得","0测试数据从左向右取得","0123456789"};
                for(int i = 0;i < srcArray.length;i ++){
                    String result = splitByLength(srcArray[i], 5);
                    System.out.println("srcArray[ " + i + "] = " + result);
                }
                //srcArray[ 0] = 测试
                //srcArray[ 1] = 0测试
                //srcArray[ 2] = 01234
            } catch (Exception e) {
                e.printStackTrace();
            }
        }    public static String splitByLength(String message, int destLength) throws Exception {
            if (message == null || message.length() <= 0 || destLength <= 0) {
                return null;
            }
            message = new String(message.getBytes(), "GB18030");
            if (message.length() <= destLength) {
                return message;
            }
            int length = 0;
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < message.length(); i++) {
                char c = message.charAt(i);
                if (isChinese(c)) {
                    length += 2;
                } else {
                    length += 1;
                }            if (length > destLength) {
                    break;
                }
                sb.append(c);
            }
            return sb.toString();
        }    public static boolean isChinese(char message) {
            boolean isChinese = false;
            if ((message < 0) || (message > 127)) {
                isChinese = true;
            }
            return isChinese;
        }
      

  12.   

    今天面试过这道题,后来想象,没有什么难的,我得出正解
    1。首先得理解一般字符编码若是英文字符,首字节首位为0,而非英文和基本字符,首字节的首位为1.
    2.从字符串的字节数组开头开始遍历数组,判断每个字节的首位是否为0,若为0循环条件递增1,若为1,循环条件递增2,当递增条件大于等于规定字节长度,跳出循环,并记住最后一次递增是1还是2,然后循环条件跟规定字节长度比较,若长度相等,ok,返回刚才遍历到的字节数组的String,若循环条件跟规定字节长度比较大1,则减去最后一个字符(根据记住最后一次递增是1还是2减去相应字节),转换成String返回。