Some text fields in my database have bad control characters embedded. I only noticed this when trying to serialize an object and get an xml error on char  and . There are probably others.
How do I replace them using C#? I thought something like this would work:
text.Replace('\x2', ' ');
but it doesn't. Any help appreciated.
Strings are immutable - you need to reassign:
text = text.Replace('\x2', ' ');
exactly as was said above, strings are immutable in C#. This means that the statement:
text.Replace('\x2', ' ');
returned the string you wanted,but didn't change the string you gave it. Since you didn't assign the return value anywhere, it was lost. That's why the statement above should fix the problem:
text = text.Replace('\x2', ' ');
If you have a string that you are frequently making changes to, you might look at the StringBuilder object, which works very much like regular strings, but they are mutable, and therefore much more efficient in some situatations.
Good luck!
-Craig
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With