I have a massive text file that I have to manually parse. There really is no other way but to iterate over it.
I'm grabbing each of the lines in the file and .split(" ") them to get the individual components - some are int arrays, others are char arrays, and some other are actual text strings.
The text strings are causing me a headache because sometimes they have a space in there.
An example line is something like:
String strLine = "Identifier {2 4 \"#0# == \\\"This String\\\"\" 12 21 6}
When I do the following:
String[] strParts = strLine.split(" ");
The resulting output is a String array that has String values of:
Identifier,{2,4,"#0#,==,\"This,String\"",12,21,6}
I need the output to be:
Identifier,{2,4,"#0# == \"This String\"",12,21,6}
So I'm pondering if there is a different whitespace character I can apply to the String inside the quotes prior to executing the split().
Anyone know of one?
I also considered diving into RegEx, but I haven't worked with RegEx enough to be able to formulate the logic around the split properly - Split on space unless that space is between the first and last quote".
Thx.
[Update]
I am adding this here because the formatting of code in replies is not optimal.
String strLine = "Identifier {2 4 \"#0# == \\\"This String\\\"\" 12 21 6}";
String delim = "§"; //use the Section Sign as a delimeter
StringBuilder sb = new StringBuilder();
//first part
sb.append(new String(strLine.substring(0,strLine.indexOf("\""))));
//middle part
sb.append(new String(strLine.substring(strLine.indexOf("\""),strLine.lastIndexOf("\""))).replace(" ", delim));
//last part
sb.append(new String(strLine.substring(strLine.lastIndexOf("\""))));
//make array
String[] parts = sb.toString().split(" ");
I'll need to replace those delimeter chars later on but at least it does what I need it to now.
Thanks all for the suggestions, it was a combination of them that ultimately solved this for me.