六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 64|回复: 0

按制定的字节截取字符串

[复制链接]

升级  30%

3

主题

3

主题

3

主题

童生

Rank: 1

积分
15
 楼主| 发表于 2013-1-27 04:46:29 | 显示全部楼层 |阅读模式
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;}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表