How to convert string to string[]? [closed]

How to convert string type to string[] type in C#?

7

11 Answers

string[] is an array (vector) of strings string is just a string (a list/array of characters)

Depending on how you want to convert this, the canonical answer could be:

string[] -> string

return String.Join(" ", myStringArray); 

string -> string[]

return new []{ myString }; 

An array is a fixed collection of same-type data that are stored contiguously and that are accessible by an index (zero based).

A string is a sequence of characters.

Hence a String[] is a collection of Strings.

For example:

String foo = "Foo"; // one instance of String String[] foos = new String[] { "Foo1", "Foo2", "Foo3" }; String firstFoo = foos[0]; // "Foo1" 

Arrays (C# Programming Guide)

Edit: So obviously there's no direct way to convert a single String to an String[] or vice-versa. Though you can use String.Split to get a String[] from a String by using a separator(for example comma).

To "convert" a String[] to a String(the opposite) you can use String.Join. You need to specify how you want to join those strings(f.e. with comma).

Here's an example:

var foos = "Foo1,Foo2,Foo3"; var fooArray = foos.Split(','); // now you have an array of 3 strings foos = String.Join(",", fooArray); // now you have the same as in the first line 
0

If you want to convert a string like "Mohammad" to String[] that contains all characters as String, this may help you:

"Mohammad".ToCharArray().Select(c => c.ToString()).ToArray() 
1

You can create a string[] (string array) that contains your string like :

string someString = "something"; string[] stringArray = new string[]{ someString }; 

The variable stringArray will now have a length of 1 and contain someString.

1

To convert a string with comma separated values to a string array use Split:

string strOne = "One,Two,Three,Four"; string[] strArrayOne = new string[] {""}; //somewhere in your code strArrayOne = strOne.Split(','); 

Result will be a string array with four strings:

{"One","Two","Three","Four"}

zerkms told you the difference. If you like you can "convert" a string to an array of strings with length of 1.

If you want to send the string as a argument for example you can do like this:

var myString = "Test"; MethodThatRequiresStringArrayAsParameter( new[]{myString} ); 

I honestly can't see any other reason of doing the conversion than to satisty a method argument, but if it's another reason you will have to provide some information as to what you are trying to accomplish since there is probably a better solution.

string is a string, and string[] is an array of strings

2

A string is one string, a string[] is a string array. It means it's a variable with multiple strings in it.

Although you can convert a string to a string[] (create a string array with one element in it), it's probably a sign that you're trying to do something which you shouldn't do.

Casting can also help converting string to string[]. In this case, casting the string with ToArray() is demonstrated:

String myString = "My String"; String[] myString.Cast<char>().Cast<string>().ToArray(); 

A string holds one value, but a string[] holds many strings, as it's an array of string.

See more here

In case you are just a beginner and want to split a string into an array of letters but didn't know the right terminology for that is a char[];

String myString = "My String"; char[] characters = myString.ToCharArray(); 

If this is not what you were looking for, sorry for wasting your time :P

You Might Also Like