HomeSearch

C# Convert Char to String

Use the string constructor or ToString to convert a char to a string.
Convert Char, String. A char can be converted to a one-character string. There are several ways to do this. The clearest method is the ToString method.
Notes, performance. Other implementations may have better performance. For example, we can use the string constructor. We test these methods.ToStringConvert
First example. Here we have a character "x" and we convert it into a string with 2 different method calls. We use ToString, and the string constructor.String Constructor
C# program that converts chars to strings using System; class Program { static void Main() { char value = 'x'; // Perform conversions. string result = value.ToString(); Console.WriteLine("RESULT LENGTH: {0}", result.Length); string result2 = new string(value, 1); Console.WriteLine("RESULT2 LENGTH: {0}", result2.Length); } } Output RESULT LENGTH: 1 RESULT2 LENGTH: 1
Benchmark. In the Main entry point, both methods return an equivalent string from the char argument. Their results are equal. We test their runtime speed.

Version 1: The first method here, Method1, uses the string constructor to convert a char to a string.

Version 2: This version of the code calls the ToString virtual method on the char type.

Result: Using the string constructor is a slightly faster approach. But other approaches, like lookup tables, are probably even better.

C# program that times char conversion using System; using System.Diagnostics; class Program { static string Method1(char value) { return new string(value, 1); } static string Method2(char value) { return value.ToString(); } const int _max = 100000000; static void Main() { Console.WriteLine(Method1('c')); Console.WriteLine(Method2('c')); var s1 = Stopwatch.StartNew(); // Version 1: use string constructor. for (int i = 0; i < _max; i++) { Method1('c'); } s1.Stop(); // Version 2: use ToString. var s2 = Stopwatch.StartNew(); for (int i = 0; i < _max; i++) { Method2('c'); } s2.Stop(); Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); Console.Read(); } } Output c c 10.37 ns 10.48 ns
Timing results. On my system, the results consistently indicated that the string constructor was slightly faster. And this held true regardless of what order the methods were tested.

Note: There is probably somewhat less overhead involved in calling the string constructor.

Lookup tables. We could implement a lookup table to get cached 1-char strings. The char could be used as an index into a precomputed array of strings.

And: This would change a string allocation into an array element load. This improves performance.

ROT13
A summary. It is simplest to just use the ToString method call. We can sometimes use Substring to take a one-character string from an input string, avoiding the char type.Substring
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls