TheDeveloperBlog.com

Home | Contact Us

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

<< Back to C-SHARP

C# Change Characters in String (ToCharArray, For Loop)

Modify many characters in a string at once using ToCharArray and a for-loop.
Change characters. Strings are immutable. If we want to change their characters, we must first convert them to arrays. Then they can be changed in memory before conversion back to a string. We show how to change string characters in this way.Strings
This example uses a series of if-statements to change characters in a string. You can make many mutations in a single pass through the string. This can improve performance. It can also make certain transformations possible.

Next: We make 3 changes: we lowercase characters, and we change the values of spaces and one letter.

ToCharArray: This method will cause an allocation upon the managed heap. Then, the new string constructor will allocate another string.

ToCharArray

Info: Compared to making multiple changes with ToLower and Replace, this approach saves allocations.

ToLowerReplace

Tip: This will reduce memory pressure and ultimately improve runtime performance.

C# program that changes characters in string using System; class Program { static void Main() { string input = "The Dev Codes"; /* * * Change uppercase to lowercase. * Change space to hyphen. * Change e to u. * * */ char[] array = input.ToCharArray(); for (int i = 0; i < array.Length; i++) { char let = array[i]; if (char.IsUpper(let)) array[i] = char.ToLower(let); else if (let == ' ') array[i] = '-'; else if (let == 'e') array[i] = 'u'; } string result = new string(array); Console.WriteLine(result); } } Output dot-nut-purls
Also, some transformations are simply not possible using standard string methods. For example using ROT13 encoding is best done with the ToCharArray and new string constructor style of code.

Tip: In my testing, using an empty character array and setting its letters one-by-one in the loop yields no performance benefit.

ROT13 Method
One letter. If you need to change one letter at a certain position (such as the first letter), this approach to string mutation is also ideal. You can call ToCharArray, then set array[0] to the new letter, and then use the new string constructor.Uppercase First LetterString Constructor
Summary. To change characters in a string, you must use a lower-level representation of the character data, which you can acquire with ToCharArray. You cannot simply assign indexes in a string.

Note: This style of method introduces complexity. It is often best to wrap this logic in a helper method, and call that method.

© TheDeveloperBlog.com
The Dev Codes

Related Links:


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