Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As I understand the issue:</p> <ul> <li>The lines in the file being parsed are NOT CSV, they are space-delimited.</li> <li>The value of the first field of each line (make/model) may contain 0 or more actual spaces.</li> <li>The values of the other fields in each line contain no spaces, so a space delimiter works fine for them.</li> </ul> <p>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.</p> <p>(Untested code)</p> <p>First, just split it:</p> <pre><code>int numFields = 4; string[] myFields = myLine.Split(' '); </code></pre> <p>Then, fix the array:</p> <pre><code>int extraSpaces = myFields.length-numFields; if(extraSpaces&gt;0) { // Piece together element 0 in the array by adding the extra elements for(int n = 1; n &lt;= 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 &lt; numFields; n++) { myFields[n] = myFields[n + extraSpaces]; } } </code></pre> <p>Finally, ignore every element of the array beyond the four you actually wanted to parse.</p> <p>Another approach would be regular expressions. I think something like this would work:</p> <pre><code> 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(); </code></pre> <p>No splitting or arrays here, just RegEx captured groups.</p> <p>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:</p> <pre><code>string[] myFields = Microsoft.VisualBasic.Replace(myLine.Reverse(), " ", "_", 0, 3).Reverse().Split(' '); myFields[0] = myFields[0].Replace("_", " "); //fix the underscores </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload