1

i want to put a string into array, suppose i have a string like string a = "dog cat horse mouse" so i want to put this string into an array like a seperate workd as it appear into the string.

string array [0]=dog 
string array [1]=cat 
string array [2]=horse 
string array [3]=mouse

or like this

string array= {"dog", "Cat", "mouse", "horse"};

I want it like this in array, so :)

asked Feb 15, 2011 at 15:36
1
  • 2
    MSDN is your friend. Use it. Become close to it. Then let SO supplement it. Commented Feb 15, 2011 at 15:43

6 Answers 6

6

You could use the Split method:

string a = "dog cat horse mouse";
string[] array = a.Split(' ');
answered Feb 15, 2011 at 15:38
Sign up to request clarification or add additional context in comments.

1 Comment

so this will put all the string from a into the array? as in this format {"dog", "Cat", "mouse"};
1

You can use the Split method:

 string s = "dog cat horse mouse";
 string[] stringArray = s.Split(' ');
 Console.WriteLine(stringArray[1]); // cat
R. Martinho Fernandes
236k73 gold badges444 silver badges518 bronze badges
answered Feb 15, 2011 at 15:39

2 Comments

why have you used single quotes in split?
A single quote tells C# to create a Char datatype, which is what the Split method expects. If you used double quotes, you would have to create a char array from it, like what was done in Billy Bob's answer.
1
string s = "dog cat horse mouse";
string[] split = s.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

StringSplitOptions.RemoveEmptyEntries will ensure that multiple spaces in the string wont return empty entries after splitting it.

answered Feb 15, 2011 at 15:45

Comments

0

You can use String.Split().

answered Feb 15, 2011 at 15:39

Comments

0

Here you go...

string a = "dog cat horse mouse"
string[] split = s.Split(" ".ToCharArray());
 //split[0] holds dog
 //split[1] holds cat
answered Feb 15, 2011 at 15:40

4 Comments

why have you use double quotes in split?
Don't you think ToCharArray is a little overkill for this?
I counted a space as a string - in C# you have to use double quotes for strings.. I believe a single quote would also work with a space however
Re ToCharArray - I just did it from memory where I have used tochararray() before. It could just be taken off!
-2

This should do the job:

string[] array="dog cat horse mouse".Split(new []{' '});
answered Feb 15, 2011 at 15:38

3 Comments

But why not just do string[] array="dog cat horse mouse".Split(' ');
@Jonners, Split can only takes an array of chars not a single char.
that line compiles and runs fine on .NET35 and .NET4... the intellisense indicates that Split demands an array, but it works with a single char.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.