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.
Printable View
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.
TryCode:string color = "red";
string location = "ocean";
String sentence = "I have a {0} fish" + color +
"\n that I caught in the {}" + location;
or try:
Code:string color = "red";
string location = "ocean";
string sentence = "I have a " + color + " fish" + Environment.NewLine +
" that I caught in the " + location;
thank you, silly question
The samples above indicates that they are using String.Format() method, so then they are wrong.
Notice that the key point is using of the placeholders {}.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);
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.Quote:
Originally Posted by DeepT
Look at Environment.NewLine.
that's why you use Environment.NewLine instead of "\n" or "\r\n".Quote:
Originally Posted by DeepT