Click to See Complete Forum and Search --> : Multiline strings with variables


skake01
July 22nd, 2008, 02:11 PM
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.

S_M_A
July 22nd, 2008, 02:17 PM
Try string color = "red";
string location = "ocean";

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

eclipsed4utoo
July 22nd, 2008, 02:49 PM
or try:


string color = "red";
string location = "ocean";

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

skake01
July 22nd, 2008, 02:55 PM
thank you, silly question

boudino
July 23rd, 2008, 01:00 AM
The samples above indicates that they are using String.Format() (http://msdn.microsoft.com/en-us/library/system.string.format.aspx) method, so then they are wrong.

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 {}.

DeepT
July 24th, 2008, 02:37 PM
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.

skake01
July 24th, 2008, 02:41 PM
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.

Arjay
July 24th, 2008, 02:50 PM
Look at Environment.NewLine (http://msdn.microsoft.com/en-us/library/system.environment.newline(VS.80).aspx).

eclipsed4utoo
July 24th, 2008, 03:06 PM
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".