Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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?
"a b" "c" "d ef"
string[]args
{"a b", "c", "d ef"}
" "
"
"a b" " " "cd"
You could use String.Split:
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);
Add a comment
This should do it:
var originalString = "\"a b\" \"c\" \"d ef\""; var args = originalString.Split('"').Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
" "(so"<space>") you could split it on that (maybe using a regexp as well) and then handle the start and end". This might turn into issues if you want to be able to have"a b" " " "cd".