Take the 2-minute tour ×

I have a messaging system that is a .NET 2 ASMX web service, very basic system. I push messages and would like to have newline symbols in the message so it's formatted on the receiving end. For example, I'd like to send a string such as: "Hello\n\nMy Name Is..." and have it have two line breaks. When the receiver reads the text it's actually outputting the \n's in the text. How can I get the \n's to be interpreted as if I was writing it in C#? Or \t etc.

Thank you.

share|improve this question
Have you tried sending \r\n instead? –  Blorgbeard May 24 '12 at 23:39
How is the 'receiving end' outputting the text? –  RJ Lohan May 24 '12 at 23:49
It sounds like your string is being escaped somewhere along the way. Have you looked at the raw text in a debugger or somwhere? I suspect you'll see either C# or html escaping in the raw text. Either way it should be simple enoungh to unescape it on the client side. –  Mike Parkhill May 24 '12 at 23:54
add comment

4 Answers

up vote 2 down vote accepted

You should compose the message using Environment.NewLine to insert the character(s) used to represent a newline on your system.

See here for references

However using Environment.NewLine has some problems.
For example, your message should be written in this way

string msg = "Hello" + Environment.NewLine + Environment.NewLine + "My Name Is...";

a bit cumbersome to use from a programmer point of view.

Then you could write an extension method for the string class which takes your message and insert at the place of a placeholder the Environment.NewLine chars.

This example use the | (pipe) character as placeholder for the newline pos.

public static string InsertLineBreaks(this string inMsg)
    Strinbuilder sb = new StringBuilder(inMsg);
    sb.Replace("|", Environment.NewLine);
    return sb.ToString();

and you can call this extension in this way

string msg = "Hello||My Name Is...".InsertLineBreaks();
share|improve this answer
That will only work if the receiving environment is the same, which can't be guaranteed by the service, right? –  RJ Lohan May 24 '12 at 23:48
@RJLohan given the tags I suppose that this was the case. However waiting for clarification from the OP –  Steve May 24 '12 at 23:52
Both sides are .NET –  Neal May 25 '12 at 0:18
it can make help in stored templates, can replace like MessageBody.Replace("{{newline}}", Environment.NewLine) ;) –  ali youhannaei Aug 18 '13 at 14:18
add comment

The web service is likely sending the message back with carriage returns listed as "%0D". What I have done in my programs is use:

str.Replace("%0D", Environment.NewLine);

That seems to work for me.

I'm sure you could also use:

str.Replace("%0D", "\n");
share|improve this answer
add comment

You have to replace the \n with something that is interpreted as a new line by the browser, i.e. <br/>. I suppose you're using it as an HTML string.

Besides you need to get sure that it isn't HtmlEncoded, i.e., that your <br/> isn't converted into &lt;br/&gt;. This shouldn't happend if you're using JSON serialization. but will happen if you serialize it as XML and isn't properly decoded in client side.

share|improve this answer
add comment

Use this:

string returnString = "Hello" + Environment.NewLine+ "My Name Is..";
share|improve this answer
add comment

Your Answer


