Home
Map
char Combine: Get String From charsCombine multiple chars into strings in various ways. Get a string from chars.
C#
This page was last reviewed on Dec 3, 2023.
Char combine. In C# several chars can be concatenated or combined into a string. We test whether the string.Concat method is useful.
Method info. We look at a way you can combine several characters. And we consider the fastest and most elegant ways of merging characters together.
string.Concat
Example. 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.
char Array
Start We call the CharCombine method by passing it 4 chars. We can adapt the code to be used on any number of chars.
Info CharCombine allocates a new char array with 4 elements. Then, it assigns slots at indices 0 to 3 to the four parameter chars.
Finally The new string constructor is invoked in the return statement, which builds the string from the char buffer we created.
String Constructor
return
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
Benchmark. 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.
Version 1 Here we call CharCombine() with 4 arguments in a tight loop. We ensure the returned strings are valid.
Version 2 The alternative here is the string.Concat method, which you can use with "+" between the chars with ToString().
Result In .NET 8, using a 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 +
Summary. 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 3, 2023 (new example).
Home
Changes
© 2007-2024 Sam Allen.