none
Split a string on a specific character in order RRS feed

  • Question

  • I have a Structured ASCII data file (LAS) with rows like
     STEP.M   0.100                          :STEP    UP_HOLE
     NULL.    -999.25                        :NULL VALUE

    I would like to split the row (string) based on the following rules:
    1) first dot in a line
    2) first space after the first dot in a line and
    3) the last colon in a line

    I have read the data in using Streamreader. How would I split the lines into an string[] based on the above rules.
    Thanks in advance

    Q

    Thursday, May 3, 2018 6:18 AM

Answers

  • Use StreamReader.ReadLine to read the lines, and check this example:

    string line = "STEP.M   0.100                          :STEP    UP_HOLE";
    
    var m = Regex.Match( line, @"(.*?)[.](.*?) (.*?):(?!.*:)(.*)" );
    if( m.Success )
    {
       string[] result = m.Groups.Cast<Group>().Skip(1).Select( g => g.Value ).ToArray();
        // . . .
    }
    

    Thursday, May 3, 2018 6:42 AM

All replies

  • Use StreamReader.ReadLine to read the lines, and check this example:

    string line = "STEP.M   0.100                          :STEP    UP_HOLE";
    
    var m = Regex.Match( line, @"(.*?)[.](.*?) (.*?):(?!.*:)(.*)" );
    if( m.Success )
    {
       string[] result = m.Groups.Cast<Group>().Skip(1).Select( g => g.Value ).ToArray();
        // . . .
    }
    

    Thursday, May 3, 2018 6:42 AM
  • Thanks Viorel.

    Works a treat.

    Thursday, May 3, 2018 6:55 AM