1

I have some data in a format like

int,int,'string',int,'string'

Every string is enclosed by '', my problems is that some strings contains commas, how can I make sure that string.split() doesn't split when inside '' ?

CloudyMarble
  • 34,949
  • 69
  • 92
  • 126

2 Answers2

0

If you are creating the data yourself you can use a different delimiter which you use as the delimiter on splitting.

Alternatively you could use the split normally with the comma as a delimiter and then go through the array and for each string entry count the number of quotes found and recombine elements containing single quote which flow eachother in the array order.

CloudyMarble
  • 34,949
  • 69
  • 92
  • 126
0

Can you use a Regular expression like "/(".*?"|[^",\s]+)(?=\s*,|\s*$)/g" and use split() and check if it works ? Something like this :

String str = "int,int,'string',int,'string','string,int'";
String[] arr = str.split("/('.*?'|[^',\\s]+)(?=\\s*,|\\s*$)/g");
for(String string:arr){
     System.out.println(string);
}
NINCOMPOOP
  • 46,742
  • 15
  • 123
  • 158