How to convert string array to string with delimiter in C#
By Tan Lee Published on Nov 06, 2024 365
To convert a string array into a single string with a delimiter in C#, you can use the string.Join method.
This method takes a delimiter and an array of strings as parameters, and it returns a single string where each element of the array is separated by the specified delimiter.
If you want to convert an array of integers to a comma-separated string.
int[] arr = new int[5] {1,2,3,4,5};
You can write like this:
var result = string.Join(",", arr);
This uses the following overload of string.Join
:
public static string Join<T>(string separator, IEnumerable<T> values);
For example:
// Define an array of strings string[] stringArray = { "apple", "banana", "cherry" }; // Convert the array to a single string with a delimiter (e.g., comma) string result = string.Join(",", stringArray); // Output // apple,banana,cherry
The string.Join(",", stringArray)
joins the elements of the stringArray
with ", "
as the delimiter.
You can replace ", "
with any other delimiter, such as " - "
or "; "
.
Categories
Popular Posts
11 Things You Didn't Know About Cloudflare
Dec 19, 2024
Modular Admin Template
Nov 14, 2024
How to secure ASP.NET Core with NWebSec
Nov 07, 2024
Login SignUp form using HTML CSS JS
Nov 11, 2024