|
-
May 12th, 2005, 02:37 AM
#1
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?
-
May 12th, 2005, 02:44 AM
#2
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";
-
May 12th, 2005, 07:12 AM
#3
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.
-
May 12th, 2005, 09:47 AM
#4
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.
-
May 12th, 2005, 09:54 AM
#5
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.
-
May 12th, 2005, 01:28 PM
#6
Re: string with new line inside resources
 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.
-
May 12th, 2005, 01:51 PM
#7
Re: string with new line inside resources
Code:
@"this is a test
new line";
-
May 12th, 2005, 01:57 PM
#8
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.
-
May 12th, 2005, 02:09 PM
#9
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();
}
}
}
-
May 13th, 2005, 02:09 AM
#10
Re: string with new line inside resources
Thanks mmetzger that works fine.
-
May 13th, 2005, 02:26 AM
#11
Re: string with new line inside resources
If you think you CAN, you can, If you think you CAN'T, you are probably right.
Have some nice Idea to share? Write an Article Online or Email to us and You may WIN a Technical Book from CG.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|