I am coding in Java and have a method that returns a string that looks something like this:
0, 2, 23131312,"This, is a message", 1212312
and I would like the string to be spit like:
["0", "2", "23131312", "This, is a message", "1212312"]
When I use the split string method on comma, it splits the "This, is a message" as as well, which I don't want. I would like it to ignore that particular comma and get rid of double quotes, if possible.
I looked up some answers and CSV seems to be the way to do it. However, I don't understand it properly.
I think you can use the regex,(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$) from here: Splitting on comma outside quotes
You can test the pattern here: http://regexr.com/3cddl
Java code example:
public static void main(String[] args) {
String txt = "0, 2, 23131312,\"This, is a message\", 1212312";
System.out.println(Arrays.toString(txt.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)")));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With