问一个字符串分割

wenxichao1 2009-03-04 08:08:56
String str="1-2$3"我要把123分出来,是不是有一个可以放多个分隔符的字符串分割方法的,忘记了,有高手指点下吗?
...全文
86 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangpeixv 2009-03-04
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 thc1987 的回复:]
正则:

Java codeimport java.util.regex.*;

public class MyRegex {
public static void main(String[] args) {
String str="1-2$3";
Matcher m=Pattern.compile("\\d").matcher(str);
String s="";
while(m.find())
s=s+m.group();

System.out.println(s);
}
}
[/Quote]
呵呵昨天刚看的TIJ
这个比较正规啊
不过能弄出来的都是好方法啦
chxy148 2009-03-04
  • 打赏
  • 举报
回复
public static void main(String args[]){
String str="1-2$3";
StringTokenizer str2 = new StringTokenizer(str,"-$");
String str3="";
while(str2.hasMoreTokens()){
str3 += str2.nextToken();
}
System.out.println(str3);
}
猿敲月下码 2009-03-04
  • 打赏
  • 举报
回复
还有简单方法:
public class MyRegex {
public static void main(String[] args) {
String str="1-2$3";
str=str.replaceAll("\\D", "");//把不是数字的变成空
System.out.println(str);
}
}
猿敲月下码 2009-03-04
  • 打赏
  • 举报
回复
正则:
import java.util.regex.*;

public class MyRegex {
public static void main(String[] args) {
String str="1-2$3";
Matcher m=Pattern.compile("\\d").matcher(str);
String s="";
while(m.find())
s=s+m.group();

System.out.println(s);
}
}
龙四 2009-03-04
  • 打赏
  • 举报
回复
String str="1-2$3";
String[] num = str.split("\\D");
「已注销」 2009-03-04
  • 打赏
  • 举报
回复
split 查看String类Api,利用正则表达式

62,623

社区成员

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

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