[.NET, Under The Hood]
This is Part 2 in the series of Joining stings
- Joining Strings - Part 1 : The + Operator
- Joining Strings - Part 2 : String.Concat
- Joining Strings - Part 3 : StringBuilder
- Joining Strings - Part 4 : String.Format
The second way to join strings is to use the
This method has several signatures that take between two and four strings.
Here is the version that takes two strings:
var son = String.Concat("Bart", "Simpson"); Console.WriteLine(son);
Here is the version that takes three strings:
var daughter = String.Concat("Lisa", "Marie", "Simpson"); Console.WriteLine(daughter);
And here is the version that takes four strings:
var mother = String.Concat("Marjorie", "Jacqueline", "Bouvier", "Simpson"); Console.WriteLine(mother);
Of course the question arises - what if you call
String.Concat with more than four strings? Can you?
var clown = String.Concat("Herschel", "Shmoikel", "Pinchas", "Yerucham", "Krustofsky"); Console.WriteLine(clown);
What happens here is that there is an overload that takes a
string array as its parameter.
There is also a version that takes an
IEmumerable<string> object as it’s parameter.
This means that you can pass anything that implements
IEnumerabe<string> such as a
var list = new List<string>(); list.Add(son); list.Add(daughter); list.Add(mother); list.Add(clown); var characters = String.Concat(list);
Of note is that there are overloads that instead of taking two to four
strings as parameters, it they take
If you are writing highly performant code using Spans, these overloads would be more appropriate choices.
The code is in my Github