|
-
July 22nd, 2008, 02:11 PM
#1
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.
-
July 22nd, 2008, 02:17 PM
#2
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;
-
July 22nd, 2008, 02:49 PM
#3
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;
-
July 22nd, 2008, 02:55 PM
#4
Re: Multiline strings with variables
thank you, silly question
-
July 23rd, 2008, 01:00 AM
#5
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. 
-
July 24th, 2008, 02:37 PM
#6
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.
-
July 24th, 2008, 02:41 PM
#7
Re: Multiline strings with variables
 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.
-
July 24th, 2008, 02:50 PM
#8
Re: Multiline strings with variables
-
July 24th, 2008, 03:06 PM
#9
Re: Multiline strings with variables
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|