[英]Split mathematical string in Java
I have this string: "23+43*435/675-23". 我有以下字符串:“ 23 + 43 * 435 / 675-23”。 How can I split it? 如何拆分? The last result which I want is: 我想要的最后一个结果是:
String 1st=23
String 2nd=435
String 3rd=675
String 4th=23
I already used this method: 我已经使用了这种方法:
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");
But it gives me this result: 但这给了我这个结果:
pLus-minuss*multi/divide
minuss*multi/divide
multi/divide
divide
But I require result in this form 但是我需要这种形式的结果
pLus
minuss
multi
divide
Try this: 尝试这个:
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);
}
Output: 输出:
23
43
435
675
23
I dont know why you want to store in variables and then print . 我不知道为什么要存储在变量中然后打印。 Anyway try below code: 无论如何尝试下面的代码:
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");
}
Output: 输出:
pLus
minuss
multi
divide
Try this out : 试试看:
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));
}
Output : 输出:
0 : 23 0:23
1 : 43 1:43
2 : 435 2:435
3 : 675 3:675
4 : 23 4:23
I think it is only the issue of index. 我认为这只是索引的问题。 You should have used index 0 to get the split result. 您应该使用索引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];
If the order of operators matters, change your code to this: 如果运算符的顺序很重要,请将您的代码更改为此:
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");
Otherwise, to spit on any operator, and store to variable do this: 否则,要吐出任何运算符并将其存储到变量中,请执行以下操作:
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");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.