Reputation:
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
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
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