86

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

2
  • 4
    arguments to methods is the only context I can think of. String.Split comes to mind (myString.Split(new[]{", "}, StringSplitOptions.RemoveEmptyEntries); for example) Commented Jun 18, 2012 at 11:15
  • @Mihir add why and how do you want to do this. Do you want split a string? "hi there Mihir" -> string.split(' ') ["hi", "there", "Mihir"]. Commented Jun 18, 2012 at 11:27

11 Answers 11

155

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 };
Sign up to request clarification or add additional context in comments.

Comments

39

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

Comments

28

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 Comment

this is what he searched for probably. One of the uses is for example in a FrameWork that accepts no 'SendKeys' but the pure raising keyboard event purely. This even only takes one byte per raise
16

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 Comment

I don't think this is the intended result.
13

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"}

1 Comment

String strOne = "One,Two,Three,Four"; String[] strArrayOne = new String[] {""}; //somewhere in your code strArrayOne = strOne.split(",");
5

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.

Comments

4

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

2 Comments

@Zerkms.. I am not only asking the difference. I asked how to convert from one to another form?
@Mihir To be fair to zerkms, you titled your question "Difference between....". The title has since been edited to match your actual question.
3

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.

Comments

3

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();

Comments

1

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

See more here

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.