string with new line inside resources
Hello,
I use ResourceManager.GetString Method to get a string from a resources file. The problem comes when I try to put new line inside the string. Get string returns the text like this
@"this is a test \r\n new line" ...
when I try to print the text it shows "\r\n" literally and does not go to new line...
Any ideas how to insert text with new lines inside it in the resources?
Re: string with new line inside resources
string s = "this is a test \r\n new line";
or
string s = @"this is a test
new line";
Re: string with new line inside resources
I mean that the ResourceManager.GetString method returns the string like this:
@"this is a test \r\n new line"
so when I try to print I dont get a new line but the \r\n comes as literals.
Re: string with new line inside resources
Well, the behaviour is perfectly ok. The @ creates a verbatim string, i.e. a string that is exactly how it looks (something like WYSIWYG).
Code:
string s = @"c:\folder\file.txt";
and
Code:
string s = "c:\\folder\\file.txt";
are the same.
In your case:
Code:
string s = @"this is a test \r\n new line";
is the same with
Code:
string s = "this is a test \\r\\n new line";
Do what MadHatter indicated.
Re: string with new line inside resources
ok but I would like to reverse the string not to be verbatium ...
The question is, how to put string with new line inside it in the resource file.
Re: string with new line inside resources
Quote:
Originally Posted by vin
I mean that the ResourceManager.GetString method returns the string like this:
@"this is a test \r\n new line"
so when I try to print I dont get a new line but the \r\n comes as literals.
you should get that..I can't get your meaning..then when you set that string to a control it's rendered as a newline.
Re: string with new line inside resources
Code:
@"this is a test
new line";
Re: string with new line inside resources
Just to clarify/add on to what the others are saying...
The @ character before a string in C# signifies that everything in between the two double quotes will be included literally into the string.
SO, if you happen to have a line break between the two quotes, that means that the line break will be included in the string. Likewise, the C# escape character ("\") will be considered not an escape character, but just a literal backslash, which means any escape sequences (n, r, etc...) after the backslash will also be considered just plain literal characters.
Re: string with new line inside resources
In other words, if I'm getting your request (ie, turn the literal \r\n back into an interpreted \r\n) try this:
Code:
string str = @"Blah Blah\r\nBlah Blah";
string newstr = str.Replace("\\r\\n", "\r\n"));
Or a full example:
Code:
using System;
using System.Text;
namespace ReplaceTextConsole
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
string str = @"Blah Blah\r\nBlah Blah";
Console.WriteLine("Literal String:");
Console.WriteLine(str);
Console.WriteLine();
Console.WriteLine("Modified String:");
Console.WriteLine(str.Replace("\\r\\n", "\r\n"));
Console.ReadLine();
}
}
}
Re: string with new line inside resources
Thanks mmetzger that works fine. :thumb:
Re: string with new line inside resources