39

I am a beginner in C# and I would like to know how to convert strings to chars, specifically string[] to char[]. I tried ToCharArray(), but I then I got an error saying that it doesn't exist. Convert.ToChar(<char here>) gives me a error saying

cannot convert from "char" to "System.Array"

5
  • You want an array of char arrays or a single array of all the chars? Nov 26, 2015 at 20:36
  • 1
    you can not convert string[] array to char[] array by using this stringValue.ToCharArray(); Nov 26, 2015 at 20:41
  • 3
    Possible Duplicate stackoverflow.com/questions/4323405/… Nov 26, 2015 at 20:47
  • Convert.ToChar() won't complain about System.Array. Please be accurate when posting here, this is unclear and wasting everyone's time.
    – H H
    Nov 26, 2015 at 20:55
  • 1
    Also, you may not even need this,you can use someString[i] to get at the chars.
    – H H
    Nov 26, 2015 at 20:56

8 Answers 8

59

Use:

string str = "Hello";
char[] characters = str.ToCharArray();

If you have a single character string, You can also try

string str = "A";
char character = char.Parse(str);

//OR 
string str = "A";
char character = str.ToCharArray()[0];
1
  • 15
    Any time you want to simply pull a single character from a string, as either a string itself, or as a char, it's usually easier, faster and more intuitive to simply dereference the position of the string directly by its index. i.e. (referring to the code above): char character = str[0];
    – NetXpert
    Feb 5, 2019 at 16:42
9

For a single string String.ToCharArray should be used

string str = "One";
var charArray = str.ToCharArray();

For an array of strings

string[] arrayStrings = { "One", "Two", "Three" };
var charArrayList = arrayStrings.Select(str => str.ToCharArray()).ToList();

For a single character from a single string:

string str = "One";
var ch = str[0]; // means 'O'
4

A string can be converted to an array of characters by calling the ToCharArray string's method.

var characters = stringValue.ToCharArray();

An object of type string[] is not a string, but an array of strings. You cannot convert an array of strings to an array of characters by just calling a method like ToCharArray. To be more correct there isn't any method in the .NET framework that does this thing. You could however declare an extension method to do this, but this is another discussion.

If your intention is to build an array of the characters that make up the strings you have in your array, you could do so by calling the ToCharArray method on each string of your array.

2
  • Strange, the tutorial I'm following had a guy converting an array of strings with char <name> = <stringValue>.ToCharArray(). For some reason visual studio won't even list "ToCharArray" as a possible method
    – enricogp
    Nov 26, 2015 at 22:26
  • Could you please share a link to this tutorial? Thanks
    – Christos
    Nov 26, 2015 at 23:28
1
char[] myChar = theString.ToCharArray();
1
  • Won't work, visual studio won't even show it as a possible method. It's like "toCharArray" doesn't exist, except it does
    – enricogp
    Nov 26, 2015 at 22:29
1
string[] array = {"USA", "ITLY"};
char[] element1 = array[0].ToCharArray();
// Now for element no 2
char[] element2 = array[1].ToCharArray();
1

Your question is a bit unclear, but I think you want (requires using System.Linq;):

var result = yourArrayOfStrings.SelectMany(s => s).ToArray();

Another solution is:

var result = string.Concat(yourArrayOfStrings).ToCharArray();
0
var theString = "TEST";
char[] myChar = theString.ToCharArray();

I tested this in the C# interactive window of Visual Studio 2019 and got:

char[4] { 'T', 'E', 'S', 'T' }
0
var theString = "1234";
char[] myChar = theString.ToCharArray();

I tested this in the C# interactive window of Visual Studio 9999 and got:

char[4] { '1', '2', '3', '4' }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.