'how to convert string to string array?
I have a object of type string,I want to convert it to String array
here the code is
obj.QueryString =HttpContext.Current.Request.Url.PathAndQuery;
string[] arr =obj.QueryString;
QueryString is of type string.
Solution 1:[1]
a string is nothing more then an array of chars, so if you want to split up the strings letters into a different string array seperatly you could do something like this:
string myString = "myString";
string[] myArray = new string[myString.Length];
for(int i = 0; i < myString.Length; i++)
{
myArray[i] = myString[i].ToString();
}
or Char Array:
string theString = "myString";
char[] theStringAsArray = theString.ToCharArray();
Solution 2:[2]
Insert whatever character you want to split on instead of the "&" argument in the Split method call.
obj.QueryString =HttpContext.Current.Request.Url.PathAndQuery;
string[] arr =obj.QueryString.Split(new char[] {'&'});
Solution 3:[3]
maybe you want to convert to char[] array instead string[] array. to do this use char[] arr = obj.QueryString.ToCharArray()
Solution 4:[4]
Here, this will make an array that may or may not fit your criteria.
var myArray = (from x in obj.QueryString select x.ToString()).ToArray()
Solution 5:[5]
You can do this compactly with Linq (similar to A.R.'s answer), but I can't speak to how efficient it is.
using System.Linq;
string input = "abcde";
var output = input.Select(char.ToString).ToArray();
> output
string[5] { "a", "b", "c", "d", "e" }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | eMi |
| Solution 2 | Steen Tøttrup |
| Solution 3 | Riccardo |
| Solution 4 | A.R. |
| Solution 5 | Rich K |
