Sunday, May 15, 2011

Tab and Newline Characters in Resource Files

Tab and newline characters cannot be added to resource files (.resx) by using “\t” or “\r\n”. This is because these escape sequences are C# specific while resource files are language independent.

Instead, you must explicitly add the tab or newline. If using the Visual Studio resource editor, you cannot insert a tab into a cell. The easiest way to get around this is to type your string (with tabs) in Notepad, then copy and paste into the resource editor. Newlines can be inserted directly into the resource editor by pressing Shift+Enter.

If you have to use the  “\t” in your resource file, then you can add code to replace the “\t” with actual tab characters:

string s = Resources.ResourceName.Replace("\\t", "\t");

Similarly, you can replace the “\n” with actual newline characters:

string s = Resources.ResourceName.Replace("\\r\\n", "\r\n");

2 comments: