为 String.Format() 自动生成占位符格式字符串

发布于 2024-08-04 21:50:24 字数 297 浏览 2 评论 0原文

有没有办法动态地告诉 String.Format() 函数(无需编写我自己的函数)有多少个占位符?我们很高兴说 15,并且知道我会为我生成 {0}-{14}。我正在生成文本文件,并且通常有超过 25 列。这会有很大帮助。

好的,

我会重新表述我的问题。我想知道是否有可能在执行时告诉 String.Format 函数我想要在格式字符串中有多少个占位符,而无需手动将它们全部输入。

根据到目前为止的回应,我猜我会继续编写自己的方法。

谢谢!

Is there a way to tell the String.Format() function (without writing my own function) how many placeholders there are dynamically? It we be great to say 15, and know I'd have {0}-{14} generated for me. I'm generate text files and I often have more than 25 columns. It would greatly help.

OK,

I will rephrase my question. I wanted to know if it is at all possible to tell the String.Format function at execution time how many place-holders I want in my format string without typing them all out by hand.

I'm guessing by the responses so far, I will just go ahead and write my own method.

Thanks!

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

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

发布评论

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

评论(4

随遇而安 2024-08-11 21:50:24

您可以使用Enumerable.RangeLINQ来生成消息字符串。

Enumerable.Range(0, 7).Select(i => "{" + i + "}").ToArray()

生成以下字符串:

"{0}{1}{2}{3}{4}{5}{6}"

You could use Enumerable.Range and LINQ to generate your message string.

Enumerable.Range(0, 7).Select(i => "{" + i + "}").ToArray()

generates following string:

"{0}{1}{2}{3}{4}{5}{6}"
|煩躁 2024-08-11 21:50:24

在 AlbertEin 的回复中添加一点,我不相信 String.Format 可以开箱即用地为您做到这一点。在使用 String.Format 方法之前,您需要动态创建格式字符串,如下所示:

var builder = new StringBuilder();

for(var i = 0; i < n; ++i)
{
  builder.AppendFormat("{0}", "{" + i + "}");
}

String.Format(builder.ToString(), ...);

但这并不完全可读。

Adding a bit to AlbertEin's response, I don't believe String.Format can do this for you out-of-the-box. You'll need to dynamically create the format string prior to using the String.Format method, as in:

var builder = new StringBuilder();

for(var i = 0; i < n; ++i)
{
  builder.AppendFormat("{0}", "{" + i + "}");
}

String.Format(builder.ToString(), ...);

This isn't exactly readable, though.

最初的梦 2024-08-11 21:50:24

当没有格式时为什么要使用 string.Format (至少从我在你的问题中看到的)?您可以使用 stringbuilder 来使用简单的串联。

Why use string.Format when there is no formatting (atleast from what I can see in your question)? You could use simple concatenation using stringbuilder instead.

·深蓝 2024-08-11 21:50:24

有一种方法可以直接执行此操作:

只需创建一个自定义 IFormatProvider,然后使用 字符串.格式

例如,如果你想始终保留 12 位小数,你可以这样做:

CultureInfo culture = Thread.CurrentThread.CurrentCulture.Clone(); // Copy your current culture
NumberFormatInfo nfi = culture.NumberFormat;
nfi.NumberDecimalDigits = 12; // Set to 12 decimal points

string newResult = string.Format(culture, "{0}", myDouble); // Will put it in with 12 decimal points

There is a way to do this directly:

Just create a custom IFormatProvider, then use this overload of string.Format.

For example, if you want to always have 12 decimal points, you can do:

CultureInfo culture = Thread.CurrentThread.CurrentCulture.Clone(); // Copy your current culture
NumberFormatInfo nfi = culture.NumberFormat;
nfi.NumberDecimalDigits = 12; // Set to 12 decimal points

string newResult = string.Format(culture, "{0}", myDouble); // Will put it in with 12 decimal points
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文