Replacing Specific Characters in a Text

by Nov 1, 2013

If you just need to replace characters anywhere in a text, you are easy off. This would capitalize every "l" in a text:

"Hello World".Replace('l', 'L') 

Sometimes, however, you may want to replace individual character at a specific position. Let's assume your text is a bit mask, and you want to set or clear a bit. This wouldn't work because it changes all bits at once:

And you cannot change characters by index, either. While you can read a character (and for example check whether a given bit is "1"), you cannot change it:

To change individual characters in a string, convert it to a string builder:

And here's how you can convert a binary from string format to decimal format:

Twitter This Tip! ReTweet this Tip!