I'm trying to get messages from Gmail in C#
but the message body comes like this "\r\n\r\nBODY\r\n\r\n"
How can I get the "BODY" out of the string ?
I tried to loop through it but I found out that when looping the "\r" becomes a " "
-
You can replace \r\n from your text file using Replace methoduser2273202– user22732022013-08-04 12:05:24 +00:00Commented Aug 4, 2013 at 12:05
5 Answers
You can use String.Trim() to remove all leading and trailing whitespace form a given string. Something like this:
var body = inputString.Trim();
This would cover /r/n characters, as well as any other whitespace.
Comments
When a user writes some text in a file, the Editor automatically appends CR|LF after each line.
CR or Carriage Return is denoted as \r
LF or Line Feed is denoted as \n
These are normally invisible to us, but while reading a text file using some code process it also captures these escape sequences.
To avoid these character you can easily replace them using builtin C# function Replace method
You can use following code to get rid from \r and \n
str = str.Replace("\r\n","");
This will first find out all combined \r\n characters and then replaces this string with "" (Blank string).