In C#, the String.Join()
method is used to concatenate an array or collection of strings into a single string, using a specified separator between each element. Here’s the basic syntax:
string result = string.Join(separator, array);
The separator
parameter is a string that is inserted between each element in the array
. The array
parameter represents the collection of strings to be joined together.
Here’s an example that demonstrates how to use String.Join()
:
string[] fruits = { "apple", "banana", "orange", "mango" }; string joinedString = string.Join(", ", fruits); Console.WriteLine(joinedString);
Output:
apple, banana, orange, mango
In this example, the fruits
array is joined into a single string using String.Join()
, with a comma and a space (", "
) as the separator. The resulting string is then printed to the console.
You can also use String.Join()
with other types of collections, such as lists or IEnumerable<T>. Here’s an example using a List<string>:
List<string> cities = new List<string> { "New York", "London", "Paris", "Tokyo" }; string joinedString = string.Join(" - ", cities); Console.WriteLine(joinedString);
Output:
New York - London - Paris - Tokyo
In this case, the List<string> “cities” is joined using ” – ” as the separator. The resulting string is then displayed.
Note that String.Join()
also has overloads that allow you to specify additional formatting options, such as formatting each element before joining them. Refer to the Microsoft documentation for more details on the available options and overloads.
Parameter:
The String.Join()
method in C# has the following parameters:
separator
(required): It is a string that specifies the separator to be inserted between each element in the array or collection of strings. The separator can be an empty string or any character(s) you choose.values
(required): It represents the array or collection of strings that you want to join together. It can be an array, a List<T>, IEnumerable<T>, or any other type that implements IEnumerable<T>.
Here’s the basic syntax again for reference:
string result = string.Join(separator, values);
Additional optional parameters:
formatProvider
(optional): It is an object that provides formatting information for individual elements in the array or collection. It allows you to specify culture-specific formatting options. By default, it uses the current culture’s formatting settings.valuesSelector
(optional): It is a delegate or lambda expression that transforms each element in the array or collection into a string representation. This allows you to apply a custom format or conversion to each element before joining them together.
Here’s an example that demonstrates the use of the optional parameters:
int[] numbers = { 1, 2, 3, 4, 5 }; string joinedString = string.Join(", ", numbers, CultureInfo.InvariantCulture, n => n.ToString("D2")); Console.WriteLine(joinedString);
Output:
01, 02, 03, 04, 05
In this example, the array of integers is joined with a comma and a space as the separator. The CultureInfo.InvariantCulture
is used for formatting, and a lambda expression n => n.ToString("D2")
is provided to format each number as a two-digit string.
Remember that the optional parameters are not required and can be omitted if you don’t need to specify custom formatting or culture-specific options.
Return:
The String.Join()
method in C# returns a new string that is the result of concatenating the elements in the specified array or collection of strings, using the specified separator.
Here’s an example that demonstrates how to capture the returned value from String.Join()
:
string[] fruits = { "apple", "banana", "orange", "mango" }; string joinedString = string.Join(", ", fruits); Console.WriteLine(joinedString);
In this example, the String.Join()
method concatenates the elements in the fruits
array with a comma and a space separator. The resulting string is assigned to the joinedString
variable. You can then use this variable as needed, such as printing it to the console or using it in further string operations.
The joinedString
variable will contain the following value:
apple, banana, orange, mango
So, the String.Join()
method returns the final concatenated string as a result, allowing you to store and manipulate the joined string as desired.
C# String Join() Method Example:
Certainly! Here’s an example that demonstrates the usage of the String.Join()
method in C#:
using System; class Program { static void Main() { string[] colors = { "red", "green", "blue" }; string joinedString = string.Join(", ", colors); Console.WriteLine(joinedString); } }
Output:
red, green, blue
In this example, we have an array called colors
containing three strings: “red”, “green”, and “blue”. We then use the String.Join()
method to concatenate the elements of the array into a single string, using a comma and a space as the separator. The resulting string is stored in the joinedString
variable.
Finally, we display the value of joinedString
using Console.WriteLine()
, which prints the following output: “red, green, blue”.