我需要有人帮助我完成一个项目,但我完全被困在这里了。
我尝试使用 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/