我正在将现有应用程序的代码转换为针对自定义硬件的 Java 1.1 编译器进行编译。这意味着我无法 String.split(regex)
将现有字符串转换为数组。
我创建了一个方法,它应该给出相同的结果,String.split(regex)
但它有问题,我不知道是什么。
代码:
private static String[] split(String delim, String line) {
StringTokenizer tokens = new StringTokenizer(line, delim, true);
String previous = "";
Vector v = new Vector();
while(tokens.hasMoreTokens()) {
String token = tokens.nextToken();
if(!",".equals(token)) {
v.add(token);
} else if(",".equals(previous)) {
v.add("");
} else {
previous = token;
}
}
return (String[]) v.toArray(new String[v.size()]);
}
样本输入:
RM^RES,0013A2004081937F,,9060,1234FF
样本输出:
String line = "RM^RES,0013A2004081937F,,9060,1234FF";
String[] items = split(",", line);
for(String s : items) {
System.out.println(" [ " + s + " ] ");
}
[ RM^RES ] [ 0013A2004081937F ] [ ] [ ] [ 9060 ] [ ] [ 1234FF ]
期望的输出:
[RM^RES][0013A2004081937F][][9060][1234FF]
我试图转换的旧代码:
String line = "RM^RES,0013A2004081937F,,9060,1234FF";
String[] items = line.split(",");
for(String s : items) {
System.out.println(" [ " + s + " ] ");
}
[RM^RES][0013A2004081937F][][9060][1234FF]