String to array?

I have a string "ab" "c" "d ef" and I would like to convert it to string[]args and have an array {"ab", "c", "d ef"} . How to disassemble it?

+4
source share
2 answers

You can use String.Split :

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

or even more efficiently:

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

This should do it:

 var originalString = "\"ab\" \"c\" \"d ef\""; var args = originalString.Split('"').Where(s => !string.IsNullOrWhiteSpace(s)).ToArray(); 
+3
source

Source: https://habr.com/ru/post/1492513/


All Articles