You have an array of strings that you want to combine and convert into a single string. This is useful for storing many values into a single database field when you have many different values in an array, among many other uses. Here we look at ways of converting strings array into strings, using different methods and comparing the precise differences between the methods in the C# language.
First, here we see an example program that has an array of five strings. It defines two methods that will convert that string array into a single string. In some cases, the first method is best, as it lets you check the strings before adding them together. In most other cases, the second method is ideal as it is shorter and simpler.
~~~ Program that converts string[] arrays (C#) ~~~
using System;
using System.Text;
class Program
{
static void Main()
{
//
// Create an array with five strings.
//
string[] array = new string[5];
array[0] = "Dot";
array[1] = "Net";
array[2] = "Perls";
array[3] = "Sam";
array[4] = "Allen";
//
// Call the methods.
//
string result1 = ConvertStringArrayToString(array);
string result2 = ConvertStringArrayToStringJoin(array);
//
// Display the results.
//
Console.WriteLine(result1);
Console.WriteLine(result2);
}
static string ConvertStringArrayToString(string[] array)
{
//
// Concatenate all the elements into a StringBuilder.
//
StringBuilder builder = new StringBuilder();
foreach (string value in array)
{
builder.Append(value);
builder.Append('.');
}
return builder.ToString();
}
static string ConvertStringArrayToStringJoin(string[] array)
{
//
// Use string Join to concatenate the string elements.
//
string result = string.Join(".", array);
return result;
}
}
~~~ Output of the program ~~~
(Note trailing period in first line.)
Dot.Net.Perls.Sam.Allen.
Dot.Net.Perls.Sam.AllenMain entry point. The Program class in the program text above defines a Main entry point first. It initializes a string array with five values. It then calls the two Conversion methods defined later in the program text. Finally, it prints the output of those methods.
Converting string array method 1. The ConvertStringArrayToString method uses an internal StringBuilder to convert the array to a string. This technique is ideal when you need to loop over your string array before adding the elements. You can actually test each individual string for some condition before appending it.
Using StringBuilder. The StringBuilder class in the base class library is ideal for appending strings in loops such as in the ConvertStringArrayToString method. It prevents many string copies from happening, and should almost always be used in loops instead of using string type appends.
Converting string array method 2. The ConvertStringArrayToStringJoin method uses the string.Join static method to convert the array to a string. This is sometimes faster than StringBuilder, and also results in shorter code. However, you cannot do any setup code before calling it, as you do not use a foreach or for loop.
Using Join. The Join method is defined on the string type, which is aliased to the System.String type in the base class library. You do not call the Join method on an actual string instance or an actual array instance. Instead, you can always type "string.Join" to call the method.
The two Convert methods shown in the program text above are static, which means they do not save state. For this reason, you can place them in a static class. However, if you specify the separator in your code, they probably will be program-specific for you, which limits their reusability. The author recommends simply directly relying on string.Join as much as possible.
Here we look at some other conversions you can perform with string arrays, strings, and char arrays. These conversions are language-specific, so you have to consult reference sources such as this site when you need to do a new conversion.
(See List and String Conversion.)
(See Convert Char Array to String.)
(See ToCharArray Method, Converting String to Array.)
Here we looked at how you can convert string arrays into strings. You can use the StringBuilder class or the static string.Join method to do this. Additionally, we reviewed these methods and described their usage on a simple string array. Finally, we noted many other common tasks and solutions with converting strings, string arrays, and char arrays.