CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2008
    Posts
    23

    Multiline strings with variables

    Very quick question, how do I do I multiline string w. variables.

    string color = red;
    string location = ocean'

    String sentence = "I have a {0} fish", color +
    "\n that I caught in the {}", location;


    I keep getting an error.

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Multiline strings with variables

    Try
    Code:
    string color = "red";
    string location = "ocean";
    
    String sentence = "I have a {0} fish" + color +
    "\n that I caught in the {}" +  location;
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Multiline strings with variables

    or try:

    Code:
    string color = "red";
    string location = "ocean";
    
    string sentence = "I have a " + color + " fish" + Environment.NewLine + 
                                 " that I caught in the " + location;

  4. #4
    Join Date
    Jul 2008
    Posts
    23

    Re: Multiline strings with variables

    thank you, silly question

  5. #5
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Multiline strings with variables

    The samples above indicates that they are using String.Format() method, so then they are wrong.
    Code:
    string color = "red";
    string location = "ocean";
    string sentence = String.Format("I have a {0} fish{2}that I caught in the {1}", color, location, Environment.NewLine);
    Notice that the key point is using of the placeholders {}.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  6. #6
    Join Date
    Sep 2004
    Posts
    1,361

    Re: Multiline strings with variables

    Beware though, in multi-line TextBoxes (and many other things) "\n" isn't enough. Sometimes you need "\r\n", sometimes you don't. If "\n" isn't working add the "\r" in front of it (order is important).

    I have not figured out the "rules" yet on this, so it is trial and error for the most part. Textfiles opened with notepad? "\n", WordPad? "\r\n". I think, or maybe it is the other way around.

  7. #7
    Join Date
    Jul 2008
    Posts
    23

    Re: Multiline strings with variables

    Quote Originally Posted by DeepT
    Beware though, in multi-line TextBoxes (and many other things) "\n" isn't enough. Sometimes you need "\r\n", sometimes you don't. If "\n" isn't working add the "\r" in front of it (order is important).

    I have not figured out the "rules" yet on this, so it is trial and error for the most part. Textfiles opened with notepad? "\n", WordPad? "\r\n". I think, or maybe it is the other way around.
    good to know.

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Multiline strings with variables


  9. #9
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Multiline strings with variables

    Quote Originally Posted by DeepT
    Beware though, in multi-line TextBoxes (and many other things) "\n" isn't enough. Sometimes you need "\r\n", sometimes you don't. If "\n" isn't working add the "\r" in front of it (order is important).

    I have not figured out the "rules" yet on this, so it is trial and error for the most part. Textfiles opened with notepad? "\n", WordPad? "\r\n". I think, or maybe it is the other way around.
    that's why you use Environment.NewLine instead of "\n" or "\r\n".

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