4

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

I want to put the string into the array after each occurance of comma not on the space. I want it like this in the array: black door,white door,red door.

2
  • 2
    What character are you splitting on? If you split on the comma (,), you should get exactly what you want. Commented Apr 27, 2011 at 11:49
  • What character are you passing into the Split method? Commented Apr 27, 2011 at 11:50

7 Answers 7

11

if you have "black door,white door,red door" string then use only , as separator

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

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

I want to put this into array
Thanks for the brief overview got it :)
7

use split like this

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

It will split only on , and not the whitespace, and should give you the expected result.

2 Comments

i am already doing like this but array split on spaces and on comma
Interesting, they comment that this doesn't work, but accept an answer that suggests precisely the same thing. Was it really the screenshot that was so convincing?
4

use ',' as separator:

s.Split(',');

1 Comment

i am already doing like this but array split on spaces and on comma
3

You need:

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

ToArray() was unnecessary.

1 Comment

@Flagbug: Spotted that at the same time!
1
string s = "black door,white door,red door";
string[] sarr;
sarr = s.Split(',');

Comments

0

Could you post your own code in its entirety? It seems we all agree that this is the proper way to do it.

Have you tried iterating through the array and printing out the values?

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

2 Comments

Nevermind, I see Stecya posted something that worked for you. But just because I'm curious, did you figure out what was causing the problem?
Kristensen, the actual problem was that i was doing it in the wrong way like i was using strings.split(char[2],',') i was just doing it because to avoid spaces but didnt knew this. :)
-1

Try this:

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

1 Comment

Well, that's because it should be ... new char[] ... instead of ... ... new char() ... (I've updated the post already). You could try resolving the syntax error by yourself first instead of complaining that it's not working ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.