I have a need to get rid of all line breaks that appear in my strings (coming from db). I do it using code below:
value.Replace("\r\n", "").Replace("\n", "").Replace("\r", "")
I can see that there's at least one character acting like line ending that survived it. The char code is 8232.
It's very lame of me, but I must say this is the first time I have a pleasure of seeing this char. It's obvious that I can just replace this char directly, but I was thinking about extending my current approach (based on replacing combinations of "\r" and "\n") to something much more solid, so it would not only include the '8232' char but also all others not-found-by-me yet.
Do you have a bullet-proof approach for such a problem?
EDIT#1:
It seems to me that there are several possible solutions:
- use Regex.Replace
- remove all chars if it's IsSeparator or IsControl
- replace with " " if it's IsWhiteSpace
- create a list of all possible line endings ( "\r\n", "\r", "\n",LF ,VT, FF, CR, CR+LF, NEL, LS, PS) and just replace them with empty string. It's a lot of replaces.
I would say that the best results will be after applying 1st and 4th approaches but I cannot decide which will be faster. Which one do you think is the most complete one?
EDIT#2
I posted anwer below.
Check out this link: http://msdn.microsoft.com/en-us/library/844skk0h.aspx
You wil lhave to play around and build a REGEX expression that works for you. But here's the skeleton...
If you've a string say "theString" then use the method Replace and give it the arguments shown below:
theString = theString.Replace(System.Environment.NewLine, "");
I'd recommend removing ALL the whitespace (char.IsWhitespace), and replacing it with single space.. IsWhiteSpace takes care of all weird unicode whitespaces.
Have you tried
string.Replace(Environment.NewLine, "")
? That usually gets a lot of them for me.Assuming that 8232 is unicode, you can do this:
8232 (0x2028) and 8233 (0x2029) are the only other ones you might want to eliminate. See the documentation for char.IsSeparator.