string into arrays
i want to put a string开发者_如何学Python 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 :)
You could use the Split method:
string a = "dog cat horse mouse";
string[] array = a.Split(' ');
You can use the Split method:
string s = "dog cat horse mouse";
string[] stringArray = s.Split(' ');
Console.WriteLine(stringArray[1]); // cat
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.
You can use String.Split().
Here you go...
string a = "dog cat horse mouse"
string[] split = s.Split(" ".ToCharArray());
//split[0] holds dog
//split[1] holds cat
This should do the job:
string[] array="dog cat horse mouse".Split(new []{' '});
精彩评论