-4

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.

sriram
  • 13
  • 1
  • 1
  • 3
  • 5
    Do you want to show us an example? – Ionică Bizău Jul 26 '12 at 12:41
  • 2
    It's a very poorly formulated question. Based on the code and with no detailed explanation, I'm guessing you'll want to String.Split. http://msdn.microsoft.com/en-us/library/system.string.split.aspx – Vedran Jul 26 '12 at 12:42
  • What should the array contain? One item containing the whole query string? The query string split by `&`s? Or split by `&`s and `=`s? Or something else? – svick Jul 26 '12 at 12:44
  • 1
    Aren't query strings usually decomposed as Maps? – npinti Jul 26 '12 at 12:44
  • @sriram see this http://stackoverflow.com/a/68648/138071 and you can convert this to array – andres descalzo Jul 26 '12 at 12:47

6 Answers6

2

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();
eMi
  • 5,540
  • 10
  • 60
  • 109
2

You can directly access the each index within the string. For example

string value = "Dot Net Perls";
char first = value[0];
char second = value[1];
char last = value[value.Length - 1];

// Write chars.
Console.WriteLine("--- 'Dot Net Perls' ---");
Console.Write("First char: ");
Console.WriteLine(first);
Console.Write("Second char: ");
Console.WriteLine(second);
Console.Write("Last char: ");
Console.WriteLine(last);

Output

--- 'Dot Net Perls' ---
First char:  D
Second char: o
Last char:   s
John Woo
  • 258,903
  • 69
  • 498
  • 492
0

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[] {'&'});
Steen Tøttrup
  • 3,755
  • 2
  • 22
  • 36
0

maybe you want to convert to char[] array instead string[] array. to do this use char[] arr = obj.QueryString.ToCharArray()

Riccardo
  • 1,490
  • 2
  • 12
  • 22
0

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()
A.R.
  • 15,405
  • 19
  • 77
  • 123
0

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" }
Rich K
  • 183
  • 2
  • 7