Does .NET 3.5 C# allow us to include a variable within a string variable without having to use the + concatenator (or string.Format(), for that matter).
For example (In the pseudo, I'm using a $ symbol to specify the variable):
DateTime d = DateTime.Now;
string s = "The date is $d";
Console.WriteLine(s);
Output:
The date is 4/12/2011 11:56:39 AM
Edit
Due to the handful of responses that suggested string.Format(), I can only assume that my original post wasn't clear when I mentioned "...(or string.Format(), for that matter)". To be clear, I'm well aware of the string.Format() method. However, in my specific project that I'm working on, string.Format() doesn't help me (it's actually worse than the + concatenator).
Also, I'm inferring that most/all of you are wondering what the motive behind my question is (I suppose I'd feel the same way if I read my question as is).
If you are one of the curious, here's the short of it:
I'm creating a web app running on a Windows CE device. Due to how the web server works, I create the entire web page content (css, js, html, etc) within a string variable. For example, my .cs managed code might have something like this:
string GetPageData()
{
string title = "Hello";
DateTime date = DateTime.Now;
string html = @"
<!DOCTYPE html PUBLIC ...>
<html>
<head>
<title>$title</title>
</head>
<body>
<div>Hello StackO</div>
<div>The date is $date</div>
</body>
</html>
";
}
As you can see, having the ability to specify a variable without the need to concatenate, makes things a bit easier - especially when the content increases in size.
Since C# 6.0 you can write string
"The title is \{title}"
which does exactly what you need.string.Format
(and similar formatting functions such asStringBuilder.AppendFormat
) are the best way to do this in terms of flexibility, coding practice, and (usually) performance:You can also specify the display format of your DateTime, as well as inserting more than one object into the string. Check out MSDN's page on the string.Format method.
Certain types also have overloads to their
ToString
methods which allow you to specify a format string. You could also create an extension method forstring
that allows you to specify a format and/or parse syntax like this.Based on the great answer of @JesperPalm I found another interesting solution which let's you use a similar syntax like in the normal
string.Format
method:This gives you the possibility to add the usual formatting:
If you are just trying to avoid concatenation of immutable strings, what you're looking for is StringBuilder.
Usage:
Or combined:
Extra info (in response to BrandonZeider):
Yep, it is kind-a important for people to realize that string conversion is automatically done. Manually adding ToString is broken, e.g.:
Also, this becomes a lot less trivial once you realize that the stringification is not equivalent to using .ToString(). You can have format specifiers, and even custom format format providers... It is interesting enough to teach people to leverage String.Format instead of doing it manually.