2

我正在将现有应用程序的代码转换为针对自定义硬件的 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]

4

5 回答 5

4

我修改了代码并测试了它。它有效(不要忘记避免对“,”进行硬编码,因此您可以将该函数用于任何分隔符):

private static String[] split(String delim, String line) {

    StringTokenizer tokens = new StringTokenizer(line, delim, true);
    String previous = delim;
    Vector v = new Vector();

    while (tokens.hasMoreTokens()) {
        String token = tokens.nextToken();

        if (!delim.equals(token)) {
            v.add(token);
        } else if (previous.equals(delim)) {
            v.add("");
        }
        previous = token;
    }

    return (String[]) v.toArray(new String[v.size()]);
}
于 2013-07-11T09:11:17.673 回答
1

几乎一切都是正确的。几乎,因为您忘记“清除” 的值previous。尝试这个:

if(!",".equals(token)) {
  v.add(token);
  previous = "";
} else if(",".equals(previous)) {
  v.add("");
  previous = "";
} else {
  previous = token;
}
于 2013-07-11T08:53:09.180 回答
0

根本不使用 StringTokenizer 怎么样:

private static String[] split(String delim, String line) {
    String current = line;
    int index = line.indexOf(delim);
    Vector vector = new Vector();
    while (index != -1) {
        vector.add(current.substring(0, index));
        current = current.substring(index + 1);
        index = current.indexOf(delim);
    }
    vector.add(current);

    return (String[]) vector.toArray(new String[vector.size()]);
}
于 2013-07-11T09:00:25.117 回答
0

你可以这样试试

 public static void main(String[] args) throws ParseException {
    for (String s : split(",", "RM^RES,0013A2004081937F, ,9060,1234FF")) {
        System.out.print(" [ " + s + " ] ");
    }
  }

private static String[] split(String delim, String line) {
    StringTokenizer tokens = new StringTokenizer(line, delim);
    String[] v = new String[tokens.countTokens()];
    int i = 0;
    while (tokens.hasMoreTokens()) {
        v[i] = tokens.nextToken();
        i++;
    }
    return v;
}
于 2013-07-11T09:01:08.927 回答
0

我认为你不应该假设任何关于底层分隔符的事情。

    public static String[] split(String line, String delim) {
        Vector v = new Vector();
        final String EMPTY_STRING = "";
        StringTokenizer st = new StringTokenizer(line, delim, true);
        while (st.hasMoreTokens()) {
            String token = st.nextToken();

            if (token.equals(delim)) {
                if (v.isEmpty() || v.size() > 0 && !EMPTY_STRING.equals(v.get(v.size() - 1))) {
                    v.add(EMPTY_STRING);
                }
            } else {
                v.add(token);
            }
        }

        return (String[])v.toArray(new String[v.size()]);
    }
于 2013-07-11T09:04:03.083 回答