我想拆分从数据库结果集行生成的以下字符串。
示例行:
A) (74,"and there","is a car","in the","garage man",t)
B) (17,account3,,,,t)
我想要如下输出(管道 | 分隔拆分元素):
A) 74 | 那里| 是一辆车| 在| 车库人| 高分辨率照片| CLIPARTO 吨
B) 17 | 帐户3 | | | | 吨
我想在我的应用程序中将零长度值存储为 null。这是我的源代码:
public void refreshSearchTable (ArrayList<String> newData){
int noOfRows = newData.size();
int noOfColumns = gui.getTableSearchResultHeader().length + 1;
Object[][] tempList = new Object[noOfRows][noOfColumns];
String rowResult = "";
String delims = "[,()\"]+";
String [] tokens;
Object [] elements = newData.toArray();
int j = 0;
for (int i = 0; i < noOfRows; i++){
rowResult = elements[i].toString();
System.out.println("rowresult: " + rowResult);
tokens = rowResult.split(delims, -1);
for (String t : tokens){
System.out.println("j: " + j + "t: " + t);
if (j == 1){ ... }
else if (j==2){ ... }
...
j++;
}
j=0;
}
}
"[,()\"]+" 适用于非零长度值。我能够根据索引值 j 拾取正确的子字符串。我读过将负值传递给 split(, ) 不会丢弃非零长度值...但确实如此。我做错了什么?