How do I remove the carriage return character (\\r)
and the new line character(\\n)
from the end of a string?
This should work ...
var tst = "12345\n\n\r\n\r\r";
var res = tst.TrimEnd( '\r', '\n' );
String temp = s.Replace("\r\n","").Trim();
s
being the original string. (Note capitals)
This was too easy -- for me I'm filtering out certain email items. I'm writing my own custom email junk filter. With \r and/or \n in the string it was wiping out all items instead of filtering.
So, I just did filter = filter.Remove('\n') and filter = filter.Remove('\r'). I'm making my filter such that an end user can use Notepad to directly edit the file so there's no telling where these characters might embed themselves -- could be other than at the start or end of the string. So removing them all does it.
The other entries all work but Remove might be the easiest?
I learned quite a bit more about Regex from this post -- pretty cool work with its use here.
varName.replace(/[\r\n]/mg, '')
For us VBers:
TrimEnd(New Char() {ControlChars.Cr, ControlChars.Lf})
string k = "This is my\r\nugly string. I want\r\nto change this. Please \r\n help!";
k = System.Text.RegularExpressions.Regex.Replace(k, @"\r\n+", " ");