您的位置:首页 > 编程语言 > Java开发

java 字符串按大小(占字节数)切分

2018-01-19 10:13 691 查看
工作中可能会遇到字符串按照占字节数大小进行切分的需求,写了一个方法,用于实现这个功能
public static List<String> splitBySize(String value, int length) {
char[] cs = value.toCharArray();
StringBuilder result = new StringBuilder();
List<String> resultList = new ArrayList<String>();
int index = 0;
for (char c: cs){
index += String.valueOf(c).getBytes().length;
if (index > length){
resultList.add(result.toString());
result.delete(0,index-1);
index = 0;
}else {
result.append(c);
}
}
return resultList;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: