Languages
[Edit]
EN

C# / .NET - string Join() method example

0 points
Created by:
Trenton-McKinney
618

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

Syntaxpublic 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.

Resulta new String that is composed of the elements separated by the separator
DescriptionThe 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

References

  1. String.Join Method (System) | Microsoft Docs 

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join