一道面试题 的正解 保证汉字不被截半个
来源:互联网 发布:json好处 编辑:程序博客网 时间:2024/06/04 19:53
- public class Test
- {
- public static void main(String[] args){
-
- String s = "我ABC汉DEF";
-
- int length=6;
- String s2 = subString(s, length);
- if(s.indexOf(s2)==-1){
- s2 = subString(s, length-1);
- };
- System.out.println("正确答案:"+s2.trim());
- }
-
- private static String subString(String s, int bl) {
- byte[] bytes = s.getBytes();
- byte[] newbytes = new byte[128];
- for ( int i =0; i<bl;i++) {
- byte b =bytes[i];
- newbytes[i]=b;
-
- }
- return new String(newbytes);
- }
- }