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.`Example.` 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.`The ToCharArray method` will cause an allocation upon the managed heap. Then, the new string constructor will allocate another string. Compared to making multiple changes with ToLower and Replace, this approach saves allocations. `This will reduce memory pressure and ultimately improve runtime performance.`ToCharArray `tochararray`ToLower `tolower`Replace `replace`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. `In my testing, using an empty character array and setting its letters one-by-one in the loop yields no performance benefit.`ROT13 Method `rot13`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 Letter `uppercase-first-letter`String Constructor `string-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. `This style of method introduces complexity. It is often best to wrap this logic in a helper method, and call that method.

123 2P2O2S2D2; { 12$1{ 112L input2z3"2("3;3 11/* 11 * 11 * Change upper2Vto lowercase. 11 * Change space2lhyphen. 11 * Change e2lu. 11 * 11 * */ 113char[] 2T2zinput.ToChar2U(); 112o(2si2z0; i < 2T.L2Z; i++) 11{2'char let2z2T[i];2'2rchar.IsUpper(let))2'12T[i]2zchar.ToLower(let);2'else 2rlet2i' ')2'12T[i]2z'-';2'else 2rlet2i'e')2'12T[i]2z'u'; 11} 112L 2I2z2w2L(2T); 112%2I); 1} } 3 dot-nut-purls3

<changes characters in string