CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Feb 2000
    Posts
    440

    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?

  2. #2
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    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";

  3. #3
    Join Date
    Feb 2000
    Posts
    440

    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.

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Feb 2000
    Posts
    440

    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.

  6. #6
    Join Date
    Sep 2004
    Location
    Tehran(Ir)
    Posts
    469

    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.

  7. #7
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: string with new line inside resources

    Code:
    @"this is a test
    new line";

  8. #8
    Join Date
    May 2005
    Posts
    54

    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.

  9. #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();
    		}
    	}
    }

  10. #10
    Join Date
    Feb 2000
    Posts
    440

    Re: string with new line inside resources

    Thanks mmetzger that works fine.

  11. #11
    Andy Tacker is offline More than "Just Another Member"
    Join Date
    Jun 2001
    Location
    55°50' N 37°39' E
    Posts
    1,503

    Re: string with new line inside resources

    now that's funny VIN...
    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
  •  





Click Here to Expand Forum to Full Width

Featured