C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Step 1: The first line in Main creates a string array with 3 elements. It specified 3 string literals.
ArrayStep 2: We call join with a lowercase "string." This is a static method. We need no string instance to call it.
StaticStep 3: We use an uppercase "String" which means the same thing. You do not need to specify a string instance.
Tip: You can specify 4 arguments on string.Join. The last two are the startIndex and the count.
OverloadC# program that joins strings
using System;
class Program
{
static void Main()
{
// Step 1: create string array.
string[] words = { "one", "two", "three" };
// Step 2: call lowercase "string" method.
Console.WriteLine(string.Join(",", words));
// Step 3: use uppercase "String" method.
Console.WriteLine(String.Join(",", words));
}
}
Output
one,two,three
one,two,three
Next: The strings are concatenated with Join into four lines of markup in HTML, separated by the BR tag.
C# program that joins HTML strings
using System;
class Program
{
static void Main()
{
// Problem: combine these words into lines in HTML
string[] dinosaurs = new string[] { "Aeolosaurus",
"Deinonychus", "Jaxartosaurus", "Segnosaurus" };
// Solution: join with break tag.
string html = string.Join("<br/>\r\n", dinosaurs);
Console.WriteLine(html);
}
}
Output
Aeolosaurus<br/>
Deinonychus<br/>
Jaxartosaurus<br/>
Segnosaurus
Part A: We call a method that combines strings with Join. A delimiter is not added onto the end.
Part B: This version of the method combines strings with StringBuilder and its Append method. A delimiter is added to the end.
Note: The end delimiter is added to the StringBuilder, but it is later removed. We call TrimEnd to remove the end delimiter.
StringBuilder ToStringTrimEnd, TrimStartC# program that combines strings with Join
using System;
using System.Text;
class Program
{
static void Main()
{
string[] animals = { "bird", "cat", "dog", "frog" };
// Part A: use method that calls string.Join.
Console.WriteLine(CombineA(animals));
// Part B: use StringBuilder method.
Console.WriteLine(CombineB(animals));
}
static string CombineA(string[] arr)
{
return string.Join(",", arr);
}
static string CombineB(string[] arr)
{
StringBuilder builder = new StringBuilder();
foreach (string s in arr)
{
builder.Append(s).Append(",");
}
return builder.ToString().TrimEnd(new char[] { ',' });
}
}
Output
bird,cat,dog,frog
bird,cat,dog,frog
Tip: This code shows what happens when you call string.Join with null parameters. It will throw an ArgumentNullException.
ExceptionC# program that throws exception on Join
using System;
class Program
{
static void Main()
{
try
{
string bug = string.Join(null, null); // Null arguments are bad
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
Output
System.ArgumentNullException: Value cannot be null.
Parameter name: value
Next: We call the string.Join<string> method. The first argument indicates the separator. The second is a reference to the List.
Returns: The method returns a joined string containing the separator. It works the same way as the array version.
Tip: This method eliminates copies. It is preferable to use this version on your List if you do not have an array of your strings handy.
C# program that joins List of strings
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Create a List of three strings.
var list = new List<string>() { "cat", "dog", "rat" };
// Join the strings from the List.
string joined = string.Join<string>("*", list);
// Display.
Console.WriteLine(joined);
}
}
Output
cat*dog*rat
Version 1: We concatenate all the strings and delimiters together with the string.Join method.
Version 2: This version of the code concatenates the strings with StringBuilder, and leaves the trailing delimiter.
StringBuilderForeachResult: The string.Join method finishes its task in less time. We should prefer string.Join when possible.
C# program that benchmarks string.Join, StringBuilder
using System;
using System.Diagnostics;
class Program
{
static string CombineA(string[] arr)
{
return string.Join(",", arr);
}
static string CombineB(string[] arr)
{
var builder = new System.Text.StringBuilder();
foreach (string s in arr)
{
builder.Append(s).Append(",");
}
return builder.ToString(); // Has ending comma.
}
const int _max = 1000000;
static void Main()
{
string[] arr = { "one", "two", "three", "four", "five" };
var s1 = Stopwatch.StartNew();
// Version 1: use string.Join.
for (int i = 0; i < _max; i++)
{
if (CombineA(arr).Length == 0)
{
return;
}
}
s1.Stop();
var s2 = Stopwatch.StartNew();
// Version 2: use StringBuilder.
for (int i = 0; i < _max; i++)
{
if (CombineB(arr).Length == 0)
{
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"));
}
}
Output
89.58 ns string.Join
141.05 ns StringBuilder, Append, ToString