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
safi
3,7868 gold badges27 silver badges38 bronze badges
-
2MSDN is your friend. Use it. Become close to it. Then let SO supplement it.Aaron McIver– Aaron McIver2011年02月15日 15:43:08 +00:00Commented Feb 15, 2011 at 15:43
6 Answers 6
You could use the Split method:
string a = "dog cat horse mouse";
string[] array = a.Split(' ');
answered Feb 15, 2011 at 15:38
Darin Dimitrov
1.0m277 gold badges3.3k silver badges3k bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
safi
so this will put all the string from a into the array? as in this format {"dog", "Cat", "mouse"};
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
John Koerner
38.2k8 gold badges93 silver badges143 bronze badges
2 Comments
safi
why have you used single quotes in split?
John Koerner
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.
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
Craigt
3,6007 gold badges43 silver badges57 bronze badges
Comments
You can use String.Split().
answered Feb 15, 2011 at 15:39
Daniel Dinu
1,76612 silver badges16 bronze badges
Comments
Here you go...
string a = "dog cat horse mouse"
string[] split = s.Split(" ".ToCharArray());
//split[0] holds dog
//split[1] holds cat
4 Comments
safi
why have you use double quotes in split?
R. Martinho Fernandes
Don't you think
ToCharArray is a little overkill for this?dotbill
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
dotbill
Re ToCharArray - I just did it from memory where I have used tochararray() before. It could just be taken off!
This should do the job:
string[] array="dog cat horse mouse".Split(new []{' '});
answered Feb 15, 2011 at 15:38
Michael Goldshteyn
74.9k25 gold badges136 silver badges183 bronze badges
3 Comments
Eight-Bit Guru
But why not just do string[] array="dog cat horse mouse".Split(' ');
Michael Goldshteyn
@Jonners, Split can only takes an array of chars not a single char.
Eight-Bit Guru
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.
lang-cs