user34537
user34537

Reputation:

string to array?

I have a string "a b" "c" "d ef" and I'd like to convert it to string[]args and have an array that is {"a b", "c", "d ef"}. How do I parse it?

Upvotes: 4

Views: 135

Answers (2)

JMK
JMK

Reputation: 28069

This should do it:

var originalString = "\"a b\" \"c\" \"d ef\"";
var args = originalString.Split('"').Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();

Upvotes: 3

Tim Schmelter
Tim Schmelter

Reputation: 460268

You could use String.Split:

string[] args = str.Split(new[]{"\" \""},StringSplitOptions.RemoveEmptyEntries)
    .Select(s => s.Trim('"')).ToArray();

or even more efficient:

args = str.Trim('"').Split(new[]{"\" \""},StringSplitOptions.RemoveEmptyEntries);

Upvotes: 5

Related Questions