java - StringTokenizer 和 Split 不起作用

标签 java android

我需要有人帮助我完成一个项目,但我完全被困在这里了。

我尝试使用 SringTokenizer:

StringTokenizer st = new StringTokenizer(auxiliar2, "+");
while (st.hasMoreElements()) {
    try{
        Textos seleccion = new Textos();
        seleccion.setBook(st.nextElement().toString());
        seleccion.setSection(st.nextElement().toString());
        seleccion.setMemorization(st.nextElement().toString());
        texto.add(seleccion);
    }finally{           
    }
}

在 StringTokenizer 中,我的第一个循环正确,但是当它执行第二个循环时,它没有找到下一个元素 seleccion.setMemorization(st.nextElement().toString()); 然后我读到 split 比我使用它效果更好。

String[] tokens = auxiliar2.split("+");
for (int x = 0; x < tokens.length-1 ; x++ ){
    Textos seleccion = new Textos();
    seleccion.setBook(tokens[x].toString());
    seleccion.setSection(tokens[x+1].toString());
    seleccion.setMemorization(tokens[x+2].toString());
    texto.add(seleccion);
    x = x+2;
}

但是这样也行不通。在这里我尝试了,但我不知道为什么只给我字符串的字符。

请问你能帮我吗?谢谢!!!!

最佳答案

split method接受正则表达式作为参数,+ 在正则表达式中具有特殊含义。

使用反斜杠\转义+,然后为Java转义反斜杠本身。

String[] tokens = auxiliar2.split("\\+");

关于java - StringTokenizer 和 Split 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19935092/

相关文章:

android - 从Android应用程序中的网站获取最新内容

java - 在线程中迭代结果集会导致异常。使用 JDBC 连接对 Hive 进行查询的结果集

java - Facebook feed 帖子上的评论/点赞操作

java - JComboBox 应用程序中的 ArrayIndexOutOfBoundsException

java - SOAP 请求 xml 内容作为字符串 : Content is not allowed in prolog and Premature end of file error

Android:布局在 gridview 的底部(在它的末尾)

java - 使用 Java 从方法调用中读取 C++ 控制台输出

java - 无法用android webview html中的图像替换字符串

android - 停止运行的前台服务

android - 应用程序发送到后台并进行响应后,firemonkey android透明度表单变为可见