- public class CutByteCode {
- public static void main(String[] args) throws IOException {
- String test = "ww琲琲你好lifang才";
- int len = test.getBytes().length;
- for(int i = 0 ; i < len; i++)
- System.out.println("截取第"+(i+1)+"字节结果是:"+cutByteMethod(test, i+1, "gbk"));
- }
-
- public static String cutByteMethod
- (String name,
- int len,
- String encode) throws IOException {
-
- byte[] buf = name.getBytes(Charset.forName(code));
- boolean flag = false;
- for(int i = 0; i < len; i++) {
- if(buf[i] < 0) {
- flag = !flag;
- if(flag&&buf[i+1]>0)
- flag = true;
- }
- else
- flag = false;
- }
- if(flag)
- return new String(buf, 0, len-1,code);
- else
- return new String(buf, 0, len, code);
- }
- }
复制代码
|