English 中文(简体)
如何在GWT中替换StringTokenzier(…,true)?
原标题:How to replace StringTokenzier(..., true) in GWT?

我正在寻找一个兼容GWT的StringTokenzier替代品,其中包含分隔符。正则表达式无法解决该任务,因为语法不是上下文无关的。

示例:提取泛型类型定义的第一级。因此,对于列表<;字符串>;,地图<;整数,映射<;字符,布尔值>>;,设置<;列表<;双倍>>,我想要一个有三个项目的列表<代码>列表<;字符串>和地图<;整数,映射<;字符,布尔值>>设置<;列表<;双倍>>

删除示例代码:

private static List<String> extractFirstLevel(String type) {
    List<String> res = new LinkedList<String>();
    StringTokenizer st = new StringTokenizer(type, "<>,", true);
    int nesting = 0;        // we are only interested in nesting 0
    String lastToken = "";
    while (st.hasMoreTokens()) {
        String token = st.nextToken();
        if (token.equals("<")) {
            nesting++;  // ignore till matching >, but keep track of additional <
            lastToken = lastToken + "<";
        } else if (token.equals(">")) {
            nesting--;  // up one level
            lastToken = lastToken + ">";
        } else if (token.equals(",")) {
            if (nesting == 0) {  // we are interested in the top level
                res.add(lastToken);
                lastToken = "";
            } else { // this is a , inside a < >, so we are not interested
                lastToken = lastToken + ", ";
            }
        } else {
            lastToken = lastToken + token.trim();
        }
    }
    res.add(lastToken);
    return res;
}
最佳回答

我最终迭代了字符串的字符:

private static List<String> extractFirstLevelNew(String type) {
    List<String> res = new LinkedList<String>();
    int start = 0;
    int nesting = 0;
    for (int i = 0; i < type.length(); i++) {
        char chr = type.charAt(i);
        if (chr ==  < ) {
            nesting++;
        } else if (chr ==  > ) {
            nesting--;
        } else if ((chr ==  , ) && (nesting == 0)) {
            res.add(type.substring(start, i).trim());
            start = i + 1;
        }
    }
    res.add(type.substring(start, type.length()).trim());
    return res;
}
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...