5

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 " "

1
  • You can replace \r\n from your text file using Replace method Commented Aug 4, 2013 at 12:05

5 Answers 5

17

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.

Sign up to request clarification or add additional context in comments.

Comments

7

try this:

string after = text.Replace(Environment.NewLine, "");

Comments

2
body = body.Replace("\r\n", string.Empty);

Comments

0

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).

Comments

0

This works for me:

    string result= rawString.Replace(@"\r\n", " ").Trim().Replace(Environment.NewLine, " ").Replace("\n", " ");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.