Char
combineIn C# several chars can be concatenated or combined into a string
. We test whether the string.Concat
method is useful.
We look at a way you can combine several characters. And we consider the fastest and most elegant ways of merging characters together.
Here we see a method that deals with char
arrays and the new string
constructor. When you need to build a string
char
-by-char
, you should use a char
array for best performance.
CharCombine
method by passing it 4 chars. We can adapt the code to be used on any number of chars.CharCombine
allocates a new char
array with 4 elements. Then, it assigns slots at indices 0 to 3 to the four parameter chars.string
constructor is invoked in the return statement, which builds the string
from the char
buffer we created.using System; class Program { public static string CharCombine(char c0, char c1, char c2, char c3) { // Allocate an array, and assign its elements to the arguments, then convert to a string. char[] array = new char[4]; array[0] = c0; array[1] = c1; array[2] = c2; array[3] = c3; return new string(array); } static void Main() { char a = 'a'; char b = 'b'; char c = 'c'; char d = 'd'; char e = 'e'; // Use CharCombine method. Console.WriteLine(CharCombine(a, b, c, d)); Console.WriteLine(CharCombine(b, c, d, e)); } }abcd bcde
What is the fastest way to convert multiple chars to a string
? This benchmark will help us learn the answer to this question. The CharCombine()
method is faster.
CharCombine()
with 4 arguments in a tight loop. We ensure the returned strings are valid.string.Concat
method, which you can use with "+" between the chars with ToString()
.char
array to combine multiple chars into a string
is significantly faster.using System; using System.Diagnostics; class Program { public static string CharCombine(char c0, char c1, char c2, char c3) { char[] array = new char[4]; array[0] = c0; array[1] = c1; array[2] = c2; array[3] = c3; return new string(array); } public static void Main() { char a = 'a'; char b = 'b'; char c = 'c'; char d = 'd'; const int _max = 1000000; var s1 = Stopwatch.StartNew(); // Version 1: combine chars with char array. for (int i = 0; i < _max; i++) { string v1 = CharCombine(a, b, c, d); string v2 = CharCombine(d, c, b, a); if (v1 != "abcd" || v2 != "dcba") { return; } } s1.Stop(); var s2 = Stopwatch.StartNew(); // Version 2: use ToString and concatenation. for (int i = 0; i < _max; i++) { string v1 = a.ToString() + b.ToString() + c.ToString() + d.ToString(); string v2 = d.ToString() + c.ToString() + b.ToString() + a.ToString(); if (v1 != "abcd" || v2 != "dcba") { return; } } 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")); } }22.27 ns CharCombine 60.78 ns +
We allocated a char
array as a buffer to store 4 chars. You can easily adapt the method to handle two, three, five, or even more chars, and it will likely perform well.