How to convert string array to string with delimiter in C#
By FoxLearn 11/6/2024 9:32:51 AM 50
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 "; "
.
- How to Call the Base Constructor in C#
- Deep Copy of Object in C#
- How to Catch Multiple Exceptions in C#
- How to cast int to enum in C#
- What is the difference between String and string in C#?
- How to retrieve the Downloads Directory Path in C#
- How to implement keyboard shortcuts in a Windows Forms application
- How to get current assembly in C#
Categories
Popular Posts
Material Lite Admin Template
11/14/2024
Freedash bootstrap lite
11/13/2024
RuangAdmin Template
11/17/2024
Responsive Animated Login Form
11/11/2024