Issue in printing Char Array() which is converted from String

12k Views Asked by At

Got stuck with this.. can you please explain what is happening in it? or give me any link!

String s1="C# Example";
Char[] s3 = s1.ToCharArray();

Console.WriteLine("S3 : {0}",s3);

I want to display the Character which is converted. Output displayed is System.Char[]. Now i need to do some changes, but what is that ?

It is possible in two ways.

1) I need to Change it to String, before i'm going to Print.

Or

2) I need to print it with Char by defining the index, (i.e) s3[0];

Am i correct. Anything More?

5

There are 5 best solutions below

3
On BEST ANSWER

Solution A:

If you want to display the characters individually on console then you need to get each character separately and display it using a loop.

foreach(char ch in  s3)
{
    Console.WriteLine("S3 : {0}", ch);
}

or, using for-loop,

for (int i = 0; i < s3.Length; i++)
{
    Console.WriteLine("S3 : {0}", s3[i]);
}

Solution B : There's anbther way that I prefer which might not be helpful for you but for those who always looks into better solutions it can be an option also.

Use Extension methods,

Add this class with the extension method in your solution,

public static class DisplayExtension 
{
    public static string DisplayResult(this string input)
    {
        var resultString = "";
        foreach (char ch in input.ToCharArray())
        {
            resultString += "S3 : " + ch.ToString() + "\n";
        }
        return resultString;
    }
}

And call the DisplayResult() extension method from your program like this,

Console.WriteLine(s1.DisplayResult());

This will give you the same result but extend the re-usability of your code without writing the for loop for all the repeated situation.

0
On
Console.WriteLine("S3 : {0}",s3);

gives result s3.ToString() which results System.Char[]

Instead create a for loop like:

Console.Write("S3 :"); 
for(int i=0; i<s3.Length; i++)
{
    Console.Write(s3[i]);
}

which gives desired output

0
On

The explanation of what happens:

  • Console.WriteLine("{0}", s3) calls s3.ToString().
    • Because WriteLine() calls ToString() on each argument
  • Method ToString() isn't overridden in type System.Array so Object.ToString() is called.
    • Because Char[] is System.Array and all types inherit from Systen.Object.
  • Which is equivalent to s3.GetType().ToString() and outputs System.Char[].
    • Because this is the default implementation. Subtypes can override it. For instance, System.String does, StringBuilder too.
1
On

Good answers so far, and great explanation from @abatishchev on why WriteLine() prints System.Char[]

How ever I would like to add an additional solution, because using loops inside your WriteLine() will look confusing and its not very pleasing to the eye. For better readability you can use new string()

In this example it would look like this:

String s1="C# Example";
Char[] s3 = s1.ToCharArray();

Console.WriteLine("S3 : {0}",new string(s3));
0
On
char [] str = new char[20];

Suppose str is the character array, and we need to display it. Do the following (provided you enter something in the str using loop):

Console.WriteLine("The string is: {0}", string.Join("",str));

Here, each character in str is joined and displayed.