按制定的字节截取字符串
public List<String> subStr(String str, int subBytes) {List<String> s = new ArrayList<String>();int bytes = 0; // 用来存储字符串的总字节数int j = 0;for (int i = 0; i < str.length(); i++) {char c = str.charAt(i);if (c < 256) {bytes += 1; // 英文字符的字节数看作1} else {bytes += 2; // 中文字符的字节数看作2}if(bytes==subBytes){s.add(str.substring(j,i+1));j=i+1;bytes=0;}else if(bytes-subBytes==1){s.add(str.substring(j, i));j=i;i--;bytes=0;}else if(str.length()-1==i&&bytes<subBytes){s.add(str.substring(j,i));}}return s;}
页:
[1]