TheDeveloperBlog.com

Home | Contact Us

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

C# ToCharArray Method, Convert String to Array

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

ToCharArray converts strings to character arrays.

It is called on a string and returns a new char array. You can manipulate this array in-place, which you can't do with a string. This makes many performance optimizations possible.

Char Array

Example. First we use ToCharArray to receive a character array from the contents of a string. The example uses an input string, and then assigns a char array to the result of the parameterless instance method ToCharArray().

Next: It uses a for-loop to get each character in the array, finally writing it to the Console.

Console

C# program that uses ToCharArray

using System;

class Program
{
    static void Main()
    {
	// Input string.
	string value = "Dot Net Perls";

	// Use ToCharArray to convert string to array.
	char[] array = value.ToCharArray();

	// Loop through array.
	for (int i = 0; i < array.Length; i++)
	{
	    // Get character from array.
	    char letter = array[i];
	    // Display each letter.
	    Console.Write("Letter: ");
	    Console.WriteLine(letter);
	}
    }
}

Output

Letter: D
Letter: o
Letter: t
Letter:
Letter: N
Letter: e
Letter: t
Letter:
Letter: P
Letter: e
Letter: r
Letter: l
Letter: s

Uses. With ToCharArray, the char array you receive is mutable. This means you can change each individual character. For this reason, the method is ideal when you need to transform characters, as in ROT13, or uppercasing the first letter.

Uppercase First LetterReverse StringROT13Char Lookup TableWhitespaceRandomize Chars in String

Also: Often you will need to change your char array back to a string. You can use the new string constructor for this.

String ConstructorConvert Char Array, String

Internals. ToCharArray uses unsafe code that manipulates pointers, along with the private wstrcpyPtrAligned method in the base class library. It is normally faster than doing the same thing in managed code, which has to check array bounds.

However: It makes a complete pass over the string, so if you do not require that, filling character arrays manually may be faster.

Summary. ToCharArray, a method on the string type, is often useful. It returns a character array filled with the string's characters. We can modify this array in-place. This improves the performance of code.

Finally: We looked at practical uses of ToCharArray and its internals in the base class library.


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