TheDeveloperBlog.com

Home | Contact Us

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

C# Convert Char Array to String

This C# program converts a char array to a string. It uses the string constructor.

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

It can be hard to remember how to do this. We review how to use the reference type string constructor. With the string constructor, we have many options available.

Constructor

Example. First, we see the approach you can use to convert the char array. This is interesting because you see the lowercase string type has a constructor. Unlike int or char, a string is a reference type. Here we initialize a char array.

Char Array

Based on:

.NET 4.5

C# program that converts char array

using System;

class Program
{
    static void Main()
    {
	// Part A: 15 character array.
	char[] c = new char[15];
	c[0] = 'O';
	c[1] = 'n';
	c[2] = 'l';
	c[3] = 'y';
	c[4] = ' ';
	c[5] = 'T';
	c[6] = 'h';
	c[7] = 'e';
	c[8] = ' ';
	c[9] = 'L';
	c[10] = 'o';
	c[11] = 'n';
	c[12] = 'e';
	c[13] = 'l';
	c[14] = 'y';

	// Part B: 15 character string.
	string s = new string(c);
	Console.WriteLine(s);
    }
}

Output

Only The Lonely

The first part shows the 15 characters in the char array assigned to letters. Recall that char is two bytes. Next, we use the new string() constructor. It is the same as a regular constructor such as new Form(), but is lowercase.

Char

String keyword. The lowercase "string" type is actually an alias for the String reference type. Strings are reference types. This means they reference external data—this is allocated on the managed heap.

Discussion. The new string constructor is overall very efficient when compared to other approaches. It is the fastest way to make a new string in many cases. I use it in my performance-sensitive web program.

String Constructor

Tip: It is much faster than manually appending characters to your string, or using StringBuilder.

StringBuilder

MSDN indicates that the String Constructor(Char[]) "Initializes a new instance of the String class to the value indicated by an array of Unicode characters." If you pass the constructor null, you get an empty string (not a null one).

String Constructor: MSDN

Summary. We used the overloaded string constructor shown to instantiate a new string object from an array. You can run the example console app to prove the method works and will work in your program.


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