S h o r t S t o r i e s

// Tales from software development

Note to self: \r\n doesn’t work in a resource string

leave a comment »

I’m surprised that I haven’t hit this before now but placing escape sequences like \r\n in a resource string doesn’t work. The reason is simple enough: these escape sequences are handled by the C# compiler when compiling C# source code. They have no effect when ‘seen’ at runtime.

You can embed a newline in a resource string using Shift+Enter but once you start thinking about it, embeddding newlines in resource strings probably isn’t such a good idea anyway. If your resource strings are translated into other languages then it might be useful to have some flexibility about where  the newlines are placed.

My solution was to add a composite formatting placeholder and use Environment.Newline to replace it.

Resource string: {0} started successfully.{1}Listening for connection requests...

Console.WriteLine(Resources.Msg0001, Application.ProductName, Environment.NewLine);

Written by Sea Monkey

March 15, 2010 at 8:00 am

Posted in Development

Tagged with ,

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: