Search code examples
c#stringselectdoublestringreader

C#, selecting certain parts from string


In my example I have a string: "POINT (6.5976512883340064 53.011505757047068)"

What I would like is to extract the two doubles from that string and place them in separate strings.

I could use a StringReader, however the doubles are not fixed in length (aka the length may vary) so I can't state after which position to start selecting.

What I would like is to state that the first selection be made after the "(" and before the whitespace, and the second selection be made after the white space and before the ")". The rest of the string can be ignored.

Any suggestions?


Solution

  • var point = "POINT (6.5976512883340064 53.011505757047068)";
    
    var indexOfFirstBrace = point.IndexOf('(') + 1;
    var indexOfLastBrace = point.IndexOf(')');
    var coordinates = point.Substring(indexOfFirstBrace, indexOfLastBrace - indexOfFirstBrace).Split(' ');
    
    var xCoordinate = coordinates[0];
    var yCoordinate = coordinates[1];