In c#, the string Join method is used to concatenate all the elements of string array using a specified separator between each element.
Following is the pictorial representation of joining two-string elements in an array with specified separator using the Join() method in c# programming language.
If you observe the above diagram, we are concatenating two array elements ”Suresh” and “Dasari” by using the Join method with defined separator “, ” and the Join method has returned the string like “Suresh, Dasari”.
Following is the syntax of defining a Join method to append or concatenate string array elements using specified separator in c# programming language.
public static string Join(string separator, params string[] values)
public static string Join(string separator, params object[] values)
public static string Join(string separator, IEnumerable<string> values)
If you observe syntaxes, the Join method will append or concatenate all the items of array or list using the specified separator between each element.
Following is the example of using the Join() method to append or concatenate all string array elements using specified separator in c# programming language.
using System;
using System.Collections.Generic;
namespace Tutlane
{
class Program
{
static void Main(string[] args)
{
string[] sArr = { "Welcome", "to", "Tutlane" };
Console.WriteLine("Join with Hypen: {0}", string.Join("-", sArr));
string[] sArr1 = { "Suresh", "Rohini", "Trishika" };
Console.WriteLine("Join with Comma: {0}", string.Join(", ", sArr1));
Console.WriteLine("\nPress Enter Key to Exit..");
Console.ReadLine();
}
}
}
If you observe above example, we used a string Join() method to append or concatenate all the elements of string array using specified separator.
When you execute the above c# program, you will get the result as shown below.
This is how you can use the string Join() method to append or concatenate all string array elements using specified separator in c# programming language.