2

I have a string like this, as you can see it consists of property/value pairs separated by space. For example: GRID "GLOBAL" or COORD 0

  GRID "GLOBAL"  LABEL "A"  DIR "X"  COORD 0  GRIDTYPE  " "

The TEXT values are inside quotation marks but the NUMERICAL ones are not.

What is the best and fastest method to split it into an array of strings like this:

  [GRID ,"GLOBAL", LABEL, "A", DIR, "X", COORD, 0, GRIDTYPE, " "] 

The below solution does not work, because there is also space between " " in the value after GRIDTYPE.

var tokens = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
Vahid
  • 5,144
  • 13
  • 70
  • 146

1 Answers1

2

This would be a good place for Regex:

string[] split = Regex.Matches(inputString, "(\\w+|\".*?\")")
                      .Cast<Match>()
                      .Select(m => m.Value)
                      .ToArray();

See it on Regex101.

Abion47
  • 22,211
  • 4
  • 65
  • 88