How to convert a string to an array?

I have a line like this string strings=" black door,white door,red door "
Now I want to put this string in an array.
I use split myarray = strings.split(',') , then the array looks like this: black,door,white,door,red,door.

I want to put a string in an array after each occurrence of a comma out of space. I want it to be in an array: black door,white door,red door.

+4
source share
7 answers

if you have the string "black door, white door, red door", use only as a delimiter

 var result = "black door,white door,red door".Split(','); 

enter image description here

+10
source

use split like this

 var result = myString.Split(','); 

It will be broken only into, not into a space, and should give you the expected result.

+7
source

use ',' as a delimiter:

 s.Split(','); 
+4
source

You need:

 var array = input.Split(','); 

ToArray () is not needed.

+3
source
 string s = "black door,white door,red door"; string[] sarr; sarr = s.Split(','); 
+1
source

Could you post your own code in its entirety? We all seem to agree that this is the right way to do this.

Have you tried iterating over an array and printing the values?

 string strings = "black door,white door,red door"; string[] myarray = strings.Split(','); foreach (string temp in myarray) { MessageBox.Show(temp); } 
0
source

Try the following:

 string input = "black door,white door,red door"; string[] values = input.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); 
-1
source

All Articles