0

我有这个字符串:“23+43*435/675-23”。我该如何拆分它?我想要的最后一个结果是:

String 1st=23
String 2nd=435
String 3rd=675
String 4th=23

我已经用过这个方法:

String s = "hello+pLus-minuss*multi/divide";
String[] split = s.split("\\+");
String[] split1 = s.split("\\-");
String[] split2 = s.split("\\*");
String[] split3 = s.split("\\/");
String plus = split[1];
String minus = split1[1];
String multi = split2[1];
String div = split3[1];
System.out.println(plus+"\n"+minus+"\n"+multi+"\n"+div+"\n");

但它给了我这个结果:

pLus-minuss*multi/divide
minuss*multi/divide
multi/divide
divide

但我需要这种形式的结果

pLus
minuss
multi
divide
4

4 回答 4

2

尝试这个:

public static void main(String[] args) {
    String s ="23+43*435/675-23";
    String[] ss = s.split("[-+*/]");
    for(String str: ss)
        System.out.println(str);

}

输出:

23
43
435
675
23

我不知道你为什么要存储在变量中然后打印。无论如何尝试下面的代码:

public static void main(String[] args) {
    String s = "hello+pLus-minuss*multi/divide";
    String[] ss = s.split("[-+*/]");
    String first =ss[1];
    String second =ss[2];
    String third =ss[3];
    String forth =ss[4];

    System.out.println(first+"\n"+second+"\n"+third+"\n"+forth+"\n");

    }

输出:

pLus
minuss
multi
divide
于 2013-03-23T11:17:56.233 回答
1

试试这个:

        String data = "23+43*435/675-23";
    Pattern pattern = Pattern.compile("[^\\+\\*\\/\\-]+");

    Matcher matcher = pattern.matcher(data);

    List<String> list = new ArrayList<String>();
    while (matcher.find()) {
        list.add(matcher.group());
    }

    for (int index = 0; index < list.size(); index++) {
        System.out.println(index + " : " + list.get(index));
    }

输出 :

0 : 23

1 : 43

2:435

3 : 675

4 : 23

于 2013-03-23T11:19:56.707 回答
0

如果运算符的顺序很重要,请将您的代码更改为:

    String s = "hello+pLus-minuss*multi/divide";
    String[] split = s.split("\\+");
    String[] split1 = split[1].split("\\-");
    String[] split2 = split1[1].split("\\*");
    String[] split3 = split2[1].split("\\/");
    String plus = split1[0];
    String minus = split2[0];
    String multi = split3[0];
    String div = split3[1];
    System.out.println(plus + "\n" + minus + "\n" + multi + "\n" + div + "\n");

否则,要吐在任何运算符上,并存储到变量中,请执行以下操作:

public static void main(String[] args) {
    String s = "hello+pLus-minuss*multi/divide";

    String[] ss = s.split("[-+*/]");
    String plus = ss[1];
    String minus = ss[2];
    String multi = ss[3];
    String div = ss[4];
    System.out.println(plus + "\n" + minus + "\n" + multi + "\n" + div + "\n");
}
于 2013-03-23T11:57:55.820 回答
0

我认为这只是索引的问题。您应该使用索引 0 来获得拆分结果。

String[] split = s.split("\\+");
String[] split1 = split .split("\\-");
String[] split2 = split1 .split("\\*");
String[] split3 = split2 .split("\\/");

String hello= split[0];//split[0]=hello,split[1]=pLus-minuss*multi/divide

String plus= split1[0];//split1[0]=plus,split1[1]=minuss*multi/divide
String minus= split2[0];//split2[0]=minuss,split2[1]=multi/divide
String multi= split3[0];//split3[0]=multi,split3[1]=divide

String div= split3[1];
于 2013-03-23T11:39:27.317 回答