是否可以在不使用连接符的情况下将 C# 变量包含在字符串变量中?
.NET 3.5 C# 是否允许我们在字符串变量中包含变量,而无需使用 + 连接符(或 string.Format())。
例如(在伪中,我使用 $ 符号来指定变量):
DateTime d = DateTime.Now;
string s = "The date is $d";
Console.WriteLine(s);
输出:
日期为 2011 年 4 月 12 日上午 11:56:39
编辑
由于少数回复建议使用 string.Format(),我只能假设我原来的帖子不清楚,当我提到“...(或 string.Format(),就此而言)”。需要明确的是,我很了解 string.Format() 方法。然而,在我正在从事的特定项目中, string.Format() 对我没有帮助(它实际上比 + 连接器更糟糕)。
另外,我推断你们大多数/所有人都想知道我的问题背后的动机是什么(我想如果我按原样阅读我的问题,我也会有同样的感觉)。
如果您对此感到好奇,请简单了解一下:
我正在创建一个在 Windows CE 设备上运行的 Web 应用程序。由于 Web 服务器的工作方式,我在字符串变量中创建整个网页内容(css、js、html 等)。例如,我的 .cs 托管代码可能具有如下内容:
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>
";
}
如您所见,能够指定变量而无需连接,使事情变得更容易 - 特别是当内容大小增加时。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
不,不幸的是 C# 不是 PHP。
但好的一面是,C# 不是 PHP。
No, unfortunately C# is not PHP.
On the bright side though, C# is not PHP.
差不多了,用一个小扩展方法。
现在我们可以写:
Almost, with a small extension method.
And now we can write:
不,事实并非如此。有很多方法可以解决这个问题,但它们违背了目的。对于你的例子来说最简单的事情是
No, it doesn't. There are ways around this, but they defeat the purpose. Easiest thing for your example is
基于@JesperPalm的出色回答,我发现了另一个有趣的解决方案,它让您可以使用与普通
string.Format
方法类似的语法:这使您可以添加常用的格式:
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:
简短的答案是:不!
The short and simple answer is: No!
不,但是您可以在字符串实例上创建一个扩展方法来缩短输入时间。
No, But you can create an extension method on the string instance to make the typing shorter.
string.Format
(以及类似的格式化函数,例如StringBuilder.AppendFormat
)在灵活性、编码实践和(通常)性能方面是实现此目的的最佳方法:还可以指定 DateTime 的显示格式,以及在字符串中插入多个对象。查看MSDN 上有关 string.Format 方法的页面。
某些类型还重载了其
ToString
方法,允许您指定格式字符串。您还可以为string
创建一个扩展方法,允许您指定格式和/或解析语法,如下所示。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.使用 T4 模板引擎怎么样?
http://visualstudiomagazine.com/articles/2009 /05/01/visual-studios-t4-code- Generation.aspx
How about using the T4 templating engine?
http://visualstudiomagazine.com/articles/2009/05/01/visual-studios-t4-code-generation.aspx
如果您只是想避免不可变字符串的串联,那么您正在寻找的是 StringBuilder.
用法:
If you are just trying to avoid concatenation of immutable strings, what you're looking for is StringBuilder.
Usage:
从 C# 6.0 开始,您可以编写字符串
"The title is \{title}"
,它正是您所需要的。Since C# 6.0 you can write string
"The title is \{title}"
which does exactly what you need.您可以使用 C# 文档中提到的类似内容。
字符串插值
you can use something like this as mentioned in C# documentation.
string interpolation
或组合:
额外信息(响应 BrandonZeider):
是的,这对于人们认识到字符串转换是自动完成的很重要。手动添加 ToString 已被破坏,例如:
此外,一旦您意识到字符串化不等于使用 .ToString(),这就变得不那么简单了。您可以拥有格式说明符,甚至自定义格式格式提供程序...教人们利用 String.Format 而不是手动执行它非常有趣。
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.