How do I display a character array? I copy a string to a character array using the 'CopyTo' method, but when I attempt to display the character array in any of the following ways:
Console.WriteLine("2nd char_array value is {0}", char_array);
Console.WriteLine("3rd char_array value is " + Convert.ToString(char_array));
string newstring;
newstring = Convert.ToString(char_array);
Console.WriteLine("4th char_array value is " + newstring);
I get the results:
2nd char_array value is System.Char[]
3rd char_array value is System.Char[]
4th char_array value is System.Char[]
Instead of the value in char_array.
Thanks for your help.
Peter Price
Loading
Vimal KandasamyPosted Apr 16, 2009, 1:09 AM
you can iterate using for loop to get char_array values
Ex:
char[] char_array ={ 'a', 'b', 'c', 'd' };
for (int i = 0; i < char_array.Length; i++)
Console.WriteLine(char_array[i]);
Here i in char_array[i] refers index value of array...
Index start with 0 and end with [no of chars in array-1];
and also you can convert it to string like below
String str = new String(char_array);
Console.WriteLine(str);
For your code
Console.WriteLine("2nd char_array value is {0}", char_array[1]);Console.WriteLine("3rd char_array value is " + Convert.ToString(char_array[2]));Console.WriteLine("4th char_array value is " + newstring);