As I understand the issue:
- The lines in the file being parsed are NOT CSV, they are space-delimited.
- The value of the first field of each line (make/model) may contain 0 or more actual spaces.
- The values of the other fields in each line contain no spaces, so a space delimiter works fine for them.
Let's say you have four columns, and the first column value is supposed to be "Nissan Almera 1.4 TDi". Using a normal Split() would result in 7 fields rather than 4.
(Untested code)
First, just split it:
int numFields = 4;
string[] myFields = myLine.Split(' ');
Then, fix the array:
int extraSpaces = myFields.length-numFields;
if(extraSpaces>0) {
// Piece together element 0 in the array by adding the extra elements
for(int n = 1; n <= extraSpaces; n++) {
myFields[0] += ' ' + myFields[n];
}
// Move the other values back to elements 1, 2, and 3 of the array
for(int n = 1; n < numFields; n++) {
myFields[n] = myFields[n + extraSpaces];
}
}
Finally, ignore every element of the array beyond the four you actually wanted to parse.
Another approach would be regular expressions. I think something like this would work:
MatchCollection m = RegEx.Matches(myLine, "^(.*) ([^ ]+) ([^ ]+) ([^ ]+)$");
string MakeModel = m.Groups[1].Captures[0].ToString();
string ModelYear = m.Groups[2].Captures[0].ToString();
string Price = m.Groups[3].Captures[0].ToString();
string NumWheels = m.Groups[4].Captures[0].ToString();
No splitting or arrays here, just RegEx captured groups.
If there were a built-in String.Reverse() method (there's not), I might suggest using VB.NET's Replace() function with the Count parameter to replace all spaces after the first three spaces (assuming four fields) in the reversed raw string, then reversing it again and splitting it. Something like:
string[] myFields = Microsoft.VisualBasic.Replace(myLine.Reverse(), " ", "_", 0, 3).Reverse().Split(' ');
myFields[0] = myFields[0].Replace("_", " "); //fix the underscores