java有关汉字字节判断的问题(面试题)

qiaoge--- 2014-07-15 10:08:30
假设现在有一个字符串"我叫ABC哈哈";
要求得到 前n个字节的字符串。n是自定义的数字。

比如:

输入1,得到的是 "我"
输入2,得到的是 "我"
输入3,得到的是 "我叫"
输入4,得到的是 "我叫"
输入5,得到的是 "我叫A"
输入6,得到的是 "我叫AB"
。。。。


应该表达清楚了, 这里只是举个例子,,


给定的字符串是 人为输入的,不是一定的
...全文
516 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
billxu1989 2014-07-22
  • 打赏
  • 举报
回复
学习了,thaks
程序男 2014-07-21
  • 打赏
  • 举报
回复
1028405943 2014-07-21
  • 打赏
  • 举报
回复
beowulf2005 2014-07-21
  • 打赏
  • 举报
回复
楼上的都没听说过CodePoint吗? Java String早就有直接方法处理CodePoint, 比如 String.codePointBefore(int); String.codePointAt(int); 多读读API没坏处。 碰到surrotage可能还是需要特别处理下。 因为某些异体字罕见字的位置比较麻烦。需要查一下。 编码这事儿水很深的。
qiaoge--- 2014-07-15
  • 打赏
  • 举报
回复
等答案,求高手解答。。。四楼的方式应该可以实现。。。有没有 更优化的解法
qiaoge--- 2014-07-15
  • 打赏
  • 举报
回复
还有没有其他的解答 方法,,,,
qiaoge--- 2014-07-15
  • 打赏
  • 举报
回复
引用 4 楼 lsongiu86 的回复:
public static String getBytes(int count,String str){
		
		byte[] bytes = str.getBytes();
		
		String str1 = new String(bytes,0,count);
		while(!str.contains(str1)){
			str1 = new String(bytes,0,++count);
		}
		return str1;
	}
pl
sca4441479 2014-07-15
  • 打赏
  • 举报
回复
引用 4 楼 lsongiu86 的回复:
厉害。
zzqcsdn123 2014-07-15
  • 打赏
  • 举报
回复
你把面试题下下来看看?
lsongiu86 2014-07-15
  • 打赏
  • 举报
回复
public static String getBytes(int count,String str){
		
		byte[] bytes = str.getBytes();
		
		String str1 = new String(bytes,0,count);
		while(!str.contains(str1)){
			str1 = new String(bytes,0,++count);
		}
		return str1;
	}
yufengdxw 2014-07-15
  • 打赏
  • 举报
回复
java里统一了汉字、英文占的字节数了吧。 n是字符结果如楼上所写,n是字节,n=1,也不可能输出“我”
tony4geek 2014-07-15
  • 打赏
  • 举报
回复
正常的是,你为什么会是上面的结果怎么区分? 我 我叫 我叫A 我叫AB 我叫ABC 我叫ABC哈
zhjdg 2014-07-15
  • 打赏
  • 举报
回复
这是自己出的吧。 条件不足。
ChongQingJin28 2014-07-15
  • 打赏
  • 举报
回复
谢 楼上 的 夸奖 与 纠正 (如果是 utf-8 i 就会 += 1 += 2 +=3 了)
vnvlyp 2014-07-15
  • 打赏
  • 举报
回复
引用 11 楼 zhouchongzxc 的回复:

public class java {

   public static String getBytes(int count,String str){
         
        byte[] bytes = str.getBytes();
         
        String str1 = new String(bytes,0,count);
        while(!str.contains(str1)){
            str1 = new String(bytes,0,++count);
        }
        return str1;
    }

   public static String getBytes2(int count,String str){
         
        byte[] bytes = str.getBytes();

	int i = 0;
        while(i<count){
            if(bytes[i] >= 0){
		i++ ;
	    }else{
		i += 2;
	    }
        }
        return new String(bytes,0,i);
    }

    public static void main(String[] args){
	String s = "我叫ABC哈哈哈";
	for(int i = 1 ;i<9;i++){
	    System.out.println(getBytes(i,s ));
	}

	for(int i = 1 ;i<9;i++){
	    System.out.println(getBytes2(i,s ));
	}
         
    }
}
getBytes2可能不是最优解但肯定是较优解了 不过这样只适合GBK等双字节编码,万一在默认是UTF-8编码的机器上以上代码就是错的 所以应该用getBytes("GBK")和new String(bytes, 0, i, "GBK")
ChongQingJin28 2014-07-15
  • 打赏
  • 举报
回复
2楼的


   public static String getBytes3(int count,String str){
         
        byte[] bytes = str.getBytes();

	int i = 0,ic = 0; 
        while(ic < count){
            if(bytes[i] >= 0){
		i++ ;
	    }else{
		i += 2;
	    }
	    ic++;
        }
        return new String(bytes,0,i);
    }
ChongQingJin28 2014-07-15
  • 打赏
  • 举报
回复

public class java {

   public static String getBytes(int count,String str){
         
        byte[] bytes = str.getBytes();
         
        String str1 = new String(bytes,0,count);
        while(!str.contains(str1)){
            str1 = new String(bytes,0,++count);
        }
        return str1;
    }

   public static String getBytes2(int count,String str){
         
        byte[] bytes = str.getBytes();

	int i = 0;
        while(i<count){
            if(bytes[i] >= 0){
		i++ ;
	    }else{
		i += 2;
	    }
        }
        return new String(bytes,0,i);
    }

    public static void main(String[] args){
	String s = "我叫ABC哈哈哈";
	for(int i = 1 ;i<9;i++){
	    System.out.println(getBytes(i,s ));
	}

	for(int i = 1 ;i<9;i++){
	    System.out.println(getBytes2(i,s ));
	}
         
    }
}
shine333 2014-07-15
  • 打赏
  • 举报
回复
4F的是我第一反应,但不是最优解

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧