TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Convert String Array to String

This C# program converts a string array into a single string.

Convert array, string. A string array can be converted into a string.

This helps store many values in a single database field. There are several ways of combining the array of strings. One way uses the string.Join method. Other ways use iteration in a loop.

Example. This example program 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.

String Array

But: In most other cases, the second method is ideal as it is shorter and simpler.

C# program that converts string arrays

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
    (Note trailing period in first line.)

Dot.Net.Perls.Sam.Allen.
Dot.Net.Perls.Sam.Allen

The Program class 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.

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

Tip: You can actually test each individual string for some condition before appending it.

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 be used in most loops.

StringBuilder

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

Join is defined on the string type, 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".

Static. The two Convert methods shown are static—they do not save state. For this reason, you can place them in a static class. If you specify the separator in your code, they probably will be program-specific, which limits their reusability.

JoinStatic

Summary. We converted string arrays into strings. You can use the StringBuilder class or the static string.Join method. We noted many other common tasks and solutions with converting strings, string arrays and char arrays.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf