We help IT Professionals succeed at work.

Replacing line breaks in string with VB/C#.NET new line

We have a series of confirmation email text stored in the web.config of various web applications.  Some web applications use VB.NET, some use C#.  The text is stored as a basic string data such as

<add key="cfgRegistrationEmailConfirmation" value="Thanks for registering with us.
      
If you have not registered to use this service, please reply to this email."

If this string is used as-is, the email text comes through like so:

Thanks for registering with us. If you have not registered to use this service, please reply to this email.

The new lines are lost.  I need to replace the text new line character with the Environment.NewLine() but am unsure how to do so.  I have tried replace "\n" but this does not do anything.

What technique should I use to maintain the formatting of these strings as stored in the configuration file for both VB.NET & C#?  Thanks in advance for any help or advice you can offer.
Comment
Watch Question

Paul MacDonaldDirector, Information Systems

Commented:
Are you sure there's actually a CRLF in the web.config and not that the line is wrapping?
You could try replacing the string in the web.config with a formatted version (note the VB.Net syntax) :
 <add key="cfgRegistrationEmailConfirmation" value="Thanks for registering with us." & vbCRLF & "If you have not registered to use this service, please reply to this email."
 

Author

Commented:
Hello:

Thanks for your response.  They are new lines, and this file is something that a non-developer is able to update, so having vbCRLF won't work as the non-developer would ultimately screw up the concatenation.  Also, I need something that will work for both C# & VB.NET.  The web.config is the one place text editors are able to put their text changes without having to send it to the developer and have it incorporated in the programming dynamically.

What I'm looking for is obviously the new lines in the text string are not represented with the Environment.NewLine because those show up correctly in the resulting email.  So I want to replace whatever is representing these new lines with the Environment.NewLine, but since "\n" does not work, I need to know how the new lines are represented.  I hope that helps.
Director, Information Systems
Commented:
Maybe it comes in as a \r rather than a \n?
You could also parse it for CHR(10) and CHR(13) and replace those with Environment.NewLine.
Top Expert 2015

Commented:
"They are new lines, and this file is something that a non-developer is able to update, so having vbCRLF won't work as the non-developer would ultimately screw up the concatenation."

Just wondering if you could use some agreed upon delimiter then split out the lines in code behind.

string textStr = "this string has two lines.~This is the second line.";
            string[] multiline = textStr.Split(new char [] {'~'});
            foreach (string line in multiline) 
            {
                Console.WriteLine(line);
            }

Open in new window

Most Valuable Expert 2011
Top Expert 2015

Commented:
What if you leave the "\n" in your config file and in code replace "\\n" with "\n". Example:
<add key="cfgRegistrationEmailConfirmation" value="Thanks for registering with us.\n\nIf you have not registered to use this service, please reply to this email." />

// Code
string setting = ConfigurationSettings.AppSettings.GetValues("cfgRegistrationEmailConfirmation")[0];

setting = setting.Replace("\\n", "\n");

Open in new window

anarki_jimbelSenior Developer

Commented:
Just in case:

Environment.NewLine Property:
"Type: System.String
A string containing "\r\n" for non-Unix platforms, or a string containing "\n" for Unix platforms."

So, your new lines can be either \n or \r\n. I believe kaufmed is right.

Really, I believe the problem has to be addressed when writing into a file, not afterwards...
Most Valuable Expert 2011
Top Expert 2015

Commented:
@anarki_jimbel

>>  Really, I believe the problem has to be addressed when writing into a file, not afterwards...

I believe his problem is that this is coming from the app/web.config file, so writing is not the issue--it's the Configuration manager which is reading the string, but disregarding line breaks in settings, apparently.