可以将参数的格式说明符作为另一个参数传递给 String.Format 吗?

发布于 2024-08-12 19:57:19 字数 350 浏览 4 评论 0原文

例如,假设我在字符串变量中有 DateTime 格式字符串,.NET 中是否有任何语法或方法可以让我执行与此无效代码等效的操作:

String line = String.Format("{0:{1}}", DateTime.Now, dateTimeFormat);
                                ^^^                        ^
                                 |                         |
                                 +-- this would use this --+

For instance, let's say I have the DateTime format-string in a string variable, is there any syntax or method in .NET that would let me do the equivalent of this invalid code:

String line = String.Format("{0:{1}}", DateTime.Now, dateTimeFormat);
                                ^^^                        ^
                                 |                         |
                                 +-- this would use this --+

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

这样的小城市 2024-08-19 19:57:19

我认为此语法概述几乎不包括动态参数。

您必须以某种方式使用两阶段系统,要么预先格式化日期,要么生成格式字符串。但我认为两者都会比问题更糟糕。

I think this syntax overview pretty much excludes dynamic parameters.

You will have to use a 2 stage system somehow, either pre-formatting your Date or by generating a format string. But I think both will be worse than the problem.

像极了他 2024-08-19 19:57:19

只是有什么问题吗

string line = DateTime.Now.ToString(dateTimeFormat);

What's wrong with just

string line = DateTime.Now.ToString(dateTimeFormat);

?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文