String str="1-2$3"我要把123分出来,是不是有一个可以放多个分隔符的字符串分割方法的,忘记了,有高手指点下吗?

解决方案 »

  1.   

    split 查看String类Api,利用正则表达式
      

  2.   

    String str="1-2$3";
    String[] num = str.split("\\D");
      

  3.   

    正则:
    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);
    }
    }
      

  4.   

    还有简单方法:
    public class MyRegex {
    public static void main(String[] args) {
    String str="1-2$3";
    str=str.replaceAll("\\D", "");//把不是数字的变成空
    System.out.println(str);
    }
    }
      

  5.   

             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);
    }
      

  6.   

    呵呵昨天刚看的TIJ
    这个比较正规啊
    不过能弄出来的都是好方法啦