先将字符串保存在容易遍历的list中,可以处理多位数字
package a;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
public class TransDemo {
public static void main(String[] args) {
String suff = "1+((2+3)*4)-5";
char c = suff.charAt(1);
List<String> li = toList(suff);
System.out.println("li = " + li);
}
private static List<String> toList(String suff) {
ArrayList<String> res = new ArrayList<>();//创建res
int i =0;//用于遍历
String whole;//用来保存多位数字
char c ;//用来保存每一个变量
while (i < suff.length()) {
if ((c = suff.charAt(i)) < 48 || (c = suff.charAt(i)) > 57) {//如果不是数
i++;
res.add(c + "");
}else {//就是数
whole ="";
do{
whole+=c;
i++;
}while (i<suff.length()&&((c = suff.charAt(i)) >= 48 && (c = suff.charAt(i)) <= 57));
res.add(whole);
}
}
return res;
}
}