菜鸟第一次参加笔试,大悲剧
可怜的应届生第一次参加笔试,见到个编程题,就是写个方法按字节截取含有中文汉字的字符串。自己想起来以前看过这个题目,主要就是碰到汉字的时候,把循环次数减一。然后就慢慢想,结果细节都忘记了,只能自己写,感觉自己能写出来。最后写出来这个
public static String cutString(String firstString, int cutNum) {char[] firstChar = firstString.toCharArray();StringBuffer secondString = new StringBuffer();for(int i = 0; i < cutNum; i++) {secondString.append(firstChar[i]);if(firstChar[i] > 'z') {cutNum--;}}return secondString.toString();}
public static String cutString(String firstString, int cutNum) {StringBuffer buff = new StringBuffer();char c;for(int i = 0; i < cutNum; i++) {c = firstString.charAt(i);buff.append(c);if(TestByte2.isChinese(c)) {cutNum--;}}return buff.toString();}public static boolean isChinese(char c) {return String.valueOf(c).getBytes().length > 1;}