I'm wanting to split a string based on white however I know that some parts of my string will be in quotes and will have spaces in it, so I don't want it to split strings that are encapsulated in double quotes.
if (file == null) return;
else
{
using (StreamReader reader = new StreamReader(file))
{
string current_line = reader.ReadLine();
string[] item;
do
{
item = Regex.Split(current_line, "\\s+");
current_line = reader.ReadLine();
echoItems(item);
}
while (current_line != null);
}
}
The split will split above will split even if it's quoted e.g "Big town" becomes in my array:
0: "big
1: town"
EDIT: after trying @vks answer, I was only able to get the IDE to accept this with all the quotes: Regex.Split(current_line, "[ ](?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
Item is an array and my print method puts a "[]" around each element when printing out the array contents. This was my output:
[0 0 0 1 2 1 1 1 "Album" 6 6 11 50 20 0 0 0 40 40 0 0 0 1 1] [] [1] [] [1] [] [1] [] [1] [] [1] [] [1] [] [1 0 0 1 3 1 1 1 "CD case" 3 3 7 20 22 0 0 0 60 0 0 0 0 1 1] [] [1] [] [1] [] [1] [] [1] [] [1] [] [1]
As you can see after splitting it is putting a large portion of the string into a single element when each of these should be broken up.
Here is a line from the file I'm trying to split:
0 0 0 1 2 1 1 1 "CD case" 6 6 11 50 20 0 0 0 40 40 0 0 0 1 1 1 1 1 1 1 1