EN
C# / .NET - string Join() method example
0
points
In this article, we would like to show you string Join() method example in C#.
Quick solution:
string joinString = String.Join(" ", "Dirask", "is", "awesome", "!");
Console.WriteLine(joinString);
// Output:
// "Dirask is awesome !"
1. Documentation
| Syntax | public static string Join(string separator, params obj[] array) |
| Parameters |
separator- a sequence of characters that is used to separate each of the elements, params - the elements to join together. |
| Result | a new String that is composed of the elements separated by the separator |
| Description | The method returns a new String composed of copies of the params elements joined together with a copy of the specified separator. |
2. Practical examples
C# string Join() method returns a string joined with a given separator which is copied for each element.
Example 1
In this example, we join string elements with an empty string as a separator.
using System;
public class Program
{
public static void Main()
{
string joinString = String.Join(" ", "Dirask", "is", "awesome", "!");
Console.WriteLine(joinString);
}
}
Output:
Dirask is awesome !
Example 2
In this example, we use Join() method with - as a separator to display string elements as a date.
using System;
public class Program
{
public static void Main()
{
string joinString = String.Join("-", "2022", "02", "28");
Console.WriteLine(joinString);
}
}
Output:
2022-02-28