Java 子串

Java 子串,java,substring,Java,Substring,我想用子串代替我的字符串,但不知道怎么做 我想要的是: 10 10 * 10 10 (这取决于用户输入) 我目前的输出是: 10 10 * 10 10 所以我缺少了*,我用一个teststring测试它,它看起来像这样: String test= "10;*;10"; System.out.println(test.substring(test.lastIndexOf(";") + 1)); System.out.println(test.substring(0, test.indexO

我想用子串代替我的字符串,但不知道怎么做

我想要的是:

10
10
*
10
10
(这取决于用户输入)

我目前的输出是:

10
10
*
10
10
所以我缺少了*,我用一个teststring测试它,它看起来像这样:

String test= "10;*;10";
System.out.println(test.substring(test.lastIndexOf(";") + 1));
System.out.println(test.substring(0, test.indexOf(";")));
如何修复字符串测试获取多个子字符串并获取输出10-*-10?

您可以使用String类中的函数

String test = "10;*;10";
String splits[] = test.split(";");

for (String split : splits) {
    System.out.println(split);
}
您可以使用string类中的函数

String test = "10;*;10";
String splits[] = test.split(";");

for (String split : splits) {
    System.out.println(split);
}

有没有不使用的理由
test.split(“;”)
我很确定您只是缺少对中间部分的解释*尝试:
System.out.println(test.substring(test.indexOf(;”),test.lastIndexOf(;”)任何不使用的原因
test.split(“;”)
我很确定您只是缺少对中间部分的解释*尝试:
System.out.println(test.substring(test.indexOf(;”),test.lastIndexOf(;”)我要么做
List splits=new ArrayList(test.split(“;”)
就是因为数组相当垃圾。显然,在这种情况下没有必要。或者,抛弃临时变量,对(字符串标记:test.split(;){
String[]splits=…
在java中更为方便,另一种方式是C风格的用法。只需一个注释。一个临时变量可以让OP理解clearlyNice,它工作得很好,谢谢。但是我正在将测试发送到服务器,服务器将他得到的项进行子串。但是当我收到数组中的所有内容时,我无法做到这一点(我猜):10*10=100您必须将字符串转换为整数才能对其执行一些计算。我要么执行
List splits=new ArrayList(test.split(;”)
仅仅因为数组是垃圾。显然,在这种情况下没有必要。或者,放弃临时变量并执行
for(字符串标记:test.split(;”)){
字符串[]splits=…
在java中更为方便,另一种方式是C风格的用法。只需一个注释。一个临时变量可以让OP理解clearlyNice,它工作得很好,谢谢。但是我正在将测试发送到服务器,服务器将他得到的项进行子串。但是当我收到数组中的所有内容时,我无法做到这一点(我猜):10*10=100必须将字符串转换为整数才能对其执行某些计算。