VB.NET New Line Character in a String

Normally I work with C# but recently I have had to do some VB.NET development. Today I came upon the issue of a new line character in a string. C# this is no issue:

string.Format("Line 1: {0}\nLine 2: {1}", str1, str2);

VB.NET this becomes a little awkward. VB.NET you have many options for a new line character:

VB Constants: vbNewLine, vbCrLf
Character Function: Chr(13)
VB Control Chars: ControlChars.NewLine, ControlChars.CrLf
Environment Variable: Environment.NewLine

Using the Environment Variable would be the recommended practice as it should return an Environment specific string. However, in practice it would come down to constants versus variables. As I saw suggested on various forums was replace the \n in your string for one of the options above. This would result in either:

String.Format("Line 1: {0}" + Environment.NewLine + "Line 2: {1}", str1, str2)  
String.Format("Line 1: {0}{1}Line 2: {2}", str1, Environment.NewLine, str2)

Neither of these options satisfied me. Since I wanted to use it in a resource file, that ruled out the first way, and the second is clumsy and error prone. There is a very simple solution to this issue though. In the resource editor, while editing the string press Shift+Enter to insert a New Line.

If you don’t have a resource file setup in your project, make sure you have Refactor! for VB.NET installed. This is free as I mentioned in my last post. Place your cursor on the string and press the Refactor! Shortcut Key Ctrl+~. Select Extract String to Resource. This will move the string literal to the resource file and replace it in the code with a call to the resource. Name the resource string appropriately, and adjust your string in the resource editor. You can now use Shift+Enter to add a new line in your string, while also using a better programming practice with resource files.

Technorati Tags:

Advertisements

7 thoughts on “VB.NET New Line Character in a String

  1. Or, you could use System.Text.RegularExpressions.Regex.Unescape(String.Format("Line 1: {0}\\nLine 2: {1}", str1, str2))That would produce the output that you were looking for. I am not sure of the implications as far as internationalization though.YMV.

  2. Hi,

    I am not sure this is still actively being looked at but need some help with string formatting in a vb .net 1.1 application I have been asked to maintain. It sends emails and is using the .AppendFormat method of the stirngbuilder class. I have been unable to brake apart two of the four lines in the body of the email. I have tried vbLf, vbLf, Chr(13), Chr(10), Environment.NewLine, vbCrLf, ControlChars.CrLf, vbNewLine none of which worked. For some reason the third line keeps wrapping up to the end of the second line. Any and all help is appreciated.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s