我在 java 中拆分字符串时遇到问题。我有
String test="if x=7 else if x=6 else if x>5 else"
String leftPart ="if"
String rightPart="else"
大家知道如何从 if 拆分为 else 并添加数组列表或字符串数组 x=7、x=6 和 x>5。
其实我想做这个
ArrayList<String>ValueArray = new ArrayList<String>();
ValueArray =[x=7,x=6,x>5];
最佳答案
试试这个..
ArrayList<String> list_array = new ArrayList<String>();
String[] string_array = test.split("else");
for(int i = 0; i < string_array.length; i++){
list_array.add((string_array[i].replace("if ","")).trim());
}
关于java - 如何拆分包含相同模式的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23798262/