I would like to split the following string generated from a database resultset row.
Sample rows:
A) (74,”and there”,”is a car”,”in the”,”garage man”,t)
B) (17,account3,,,,t)
I want an output as follows (Pipe | to separate split elements):
A) 74 | and there | is a car | in the | garage man | t
B) 17 | account3 | | | | t
I want to zero length values to be stored as null in my application. This is my source code:
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;
}
}
“[,()\”]+” works fine for non-zero length values. I’m able to pick up the right sub-string according to index value j. I’ve read that passing a negative value to split(, ) will not discard the non-zero length values … but it does. What am i doing wrong?
Maybe you could try with
.split("[()\"]*,{1}[()\"]*");The expression user before
[,()\"]+will use as separator more than one comma or any other separator informed. For instance,,will be consider one, so does[],[]. The expression commented above, expects that zero or more of the separators are followed with just one comma and this can be followed with zero or more separators too.