字符串处理算法

diracstar 2007-08-19 09:35:34
原始字符串:String src="1,2,3,4,5,6,7,8"
场景1:String自有的方法public String [] split(String regex)
应用:String [] target=src.split(",")
结果:target=["1","2","3","4","5","6","7","8"]
场景二:要求扩展String自有的split(String regex)
定义函数 public String [] splitString(String src,String regx,int num)
应用:String [] target = splitString(src,",",3);
结果:target =["1,2,3","4,5,6","7,8"]

我的思路是:先用String 自有的split方法分割,然后再重组成一个字符串数组,返回,但是感觉效率很低.

请教大家有什么好的算法;




...全文
215 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
andycpp 2007-08-19
  • 打赏
  • 举报
回复
呵呵 是我脑子没转过来,修正如下:

public static String[] splitString(String src,String regex,int num) {
List<String> l = new ArrayList<String>();
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(src);
int beg = 0;
int count = 0;
while(m.find()) {
if(++count == num) {
l.add(src.substring(beg, m.start()));
beg = m.end();
count = 0 ;
}
}
if(beg<src.length())
l.add(src.substring(beg));

return l.toArray(new String[0]);
}
diracstar 2007-08-19
  • 打赏
  • 举报
回复
src="1,2,3,4,5,6,7,8,9"
split(src,",",2)
输出
1,2
3,4
5,6
7,8

9 没有掉啦
diracstar 2007-08-19
  • 打赏
  • 举报
回复
感谢andycpp的帮助
对正则表达式 我还不怎么会用,学习下.

andycpp上面的代码好要处理一下边际数据
如String src="1,2,3,4,5,6,7,8"
如果指定num=3
则7,8丢失
andycpp 2007-08-19
  • 打赏
  • 举报
回复
有一点是需要记住的
regex参数代表了正则表达式,而不是普通的一个标点符号
andycpp 2007-08-19
  • 打赏
  • 举报
回复
public static String[] splitString(String src,String regex,int num) {
List<String> l = new ArrayList<String>();
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(src);
int beg = 0;
int count = 0;
while(m.find()) {
if(++count == num) {
l.add(src.substring(beg, m.start()));
beg = m.end();
count = 0 ;
}
}
if(count > 0) {
l.add(src.substring(beg));
}

return l.toArray(new String[0]);
}


楼主可以用这个代码片段测试一下这个程序:

public static void main(String[] args) {

String test = "aaa..bbb,,cccccc??ddd!!ee..ffff,,gg??hhhhh!!kk..llll,,mmm";
String[] result = splitString(test, "\\.\\.|,,|\\?\\?|!!", 4);
for(String s:result) {
System.out.println(s);
}
System.out.println("程序结束!!");

}
diracstar 2007-08-19
  • 打赏
  • 举报
回复
这是我写的,结果是对的,但是感觉很丑陋.
public static String[] split(String src, String regex, int num) {
String[] temp = src.split(regex);
int redunt = temp.length % num;
int total = temp.length / num;
if (redunt != 0) {
total++;
}
String[] target = new String[total];
int count = 0;
for (int i = 0; i < temp.length;) {
StringBuffer sb = new StringBuffer();
if (i == temp.length - 1) {
sb.append(temp[i++]);
} else {
for (int j = 0; j < num; j++) {
if (j == num - 1) {
sb.append(temp[i++]);
} else {
sb.append(temp[i++]);
sb.append(regex);
}
}
}
target[count++] = sb.toString();
}
return target;
}
zapdos 2007-08-19
  • 打赏
  • 举报
回复
遍历String一下,把index%3==2的,位置记下来,改成其它特殊的符号,再split2次就可以了
效率应该不会很差
andycpp 2007-08-19
  • 打赏
  • 举报
回复
不对,没想仔细
用String的split函数是不行的

比如你传入的正则表达式是“[,.!?]”
你先用当然要用String的split函数分割后,就失去了具体的分隔符信息
再组合的时候,你使用句号组合呢,还是逗号组合呢,还是叹号啊?

还是自己用正则表达式认真写吧
andycpp 2007-08-19
  • 打赏
  • 举报
回复
当然要用String的split函数
因为参数中有正则表达式啊
难道你要自己写一个正则表达式解析器??????

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧