char ch='\'';
that is single cote \ single cote single cote.
that is single cote \ single cote single cote.
Program that joins strings [C#]
using System;
class Program
{
static void Main()
{
string[] arr = { "one", "two", "three" };
Console.WriteLine(string.Join(",", arr)); // "string" can be lowercase, or
Console.WriteLine(String.Join(",", arr)); // "String" can be uppercase
}
}
Output
one,two,three
one,two,threeDescription of the example code. Program that joins HTML strings [C#]
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/>
SegnosaurusDescription of the example code. There is a string[] array declared at the beginning of the code. Those strings are concatenated with Join into four lines of markup in HTML, separated by the BR tag.
using System;
using System.Text;
class Program
{
static void Main()
{
string[] catSpecies = { "Aegean", "Birman", "Main Coon", "Nebulung" };
Console.WriteLine(CombineA(catSpecies));
Console.WriteLine(CombineB(catSpecies));
}
/// <summary>
/// Combine strings with commas.
/// </summary>
static string CombineA(string[] arr)
{
return string.Join(",", arr);
}
/// <summary>
/// Combine strings with commas.
/// </summary>
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
Aegean,Birman,Main Coon,Nebulung
Aegean,Birman,Main Coon,NebulungDescription of the example code. As noted, the two methods CombineA and CombineB both concatenate each string into a single string with separators. The species of cats are outputted as a single string. The final method shown above, CombineB, has to use the ToString() and TrimEnd() methods to convert the StringBuilder into the result.Program that throws exception on Join [C#]
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: valueDescription of the example code. This code demonstrates what happens when you call string.Join with null parameters. It will throw the ArgumentNullException. Depending on your application, this must be dealt with.Program that joins on List [C#]
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// List of cities
List<string> cities = new List<string>();
cities.Add("New York");
cities.Add("Mumbai");
cities.Add("Berlin");
cities.Add("Istanbul");
// Join strings into one CSV line
string line = string.Join(",", cities.ToArray());
Console.WriteLine(line);
}
}
Output
New York,Mumbai,Berlin,IstanbulDescription of the example code. The Main method first creates a List of strings, and then converts the List into a string[] array using ToArray(). string.Join returns all the strings combined into one with separators.Data used in benchmark
string[] arr = { "one", "two", "three", "four", "five" };
Methods that were benchmarked [C#]
1000000 iterations were tested.
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 [difference]
}
Results
string.Join: 157 ms [faster]
StringBuilder Append method: 270 ms
Required Join method results
Input: one
two
three
Output: one*two*threeDescription of benchmarked code. The two methods shown above, CombineA and CombineB, compare string.Join to a StringBuilder loop. They return different strings: CombineA does not have a comma at the end of its result, while CombineB does. Using TrimEnd to remove the comma makes CombineB slower.