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:
xxxxxxxxxx
1
string joinString = String.Join(" ", "Dirask", "is", "awesome", "!");
2
3
Console.WriteLine(joinString);
4
5
// Output:
6
// "Dirask is awesome !"
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. |
C# string Join()
method returns a string joined with a given separator which is copied for each element.
In this example, we join string elements with an empty string as a separator.
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
public static void Main()
6
{
7
string joinString = String.Join(" ", "Dirask", "is", "awesome", "!");
8
9
Console.WriteLine(joinString);
10
}
11
}
Output:
xxxxxxxxxx
1
Dirask is awesome !
In this example, we use Join()
method with - as a separator to display string elements as a date.
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
public static void Main()
6
{
7
string joinString = String.Join("-", "2022", "02", "28");
8
9
Console.WriteLine(joinString);
10
}
11
}
Output:
xxxxxxxxxx
1
2022-02-28