字符串 {0} 中的大括号

发布于 2024-10-24 17:11:48 字数 179 浏览 7 评论 0原文

我经常在通常包含数字的字符串中看到大括号,例如:

string something = "I have {0} cats";

虽然我可以弄清楚这意味着什么,但我可以说我从未阅读过任何与其用法相关的文档。 C# 字符串文档似乎没有与这些相关的任何信息。有人能指出我正确的方向吗?

I often see curly braces in a string usually containing a number, such as:

string something = "I have {0} cats";

Whilst I can work out what this means, I can say I've never read any documentation relating to its useage. The c# string documentation seems to be void of any information relating to these. Can anyone point me in the right direction?

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

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

发布评论

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

评论(6

呆头 2024-10-31 17:11:48

在 string.Format 中用作值参数的占位符。 string.Format("我有 {0} 只猫", 5);打印“我有 5 只猫”

所以你可以使用 string.Format(something, 5);并得到与上面相同的结果

Used in string.Format as a place holder for a value parameter. string.Format("I have {0} cats", 5); prints "I have 5 cats"

So you could use string.Format(something, 5); and get the same result as above

莫相离 2024-10-31 17:11:48

这是 String.Format,称为“复合格式”。有关详细信息,请查看此处

It's the normal format string used by String.Format, and is called "composite formatting". For more info about it, have a look here.

陌伤浅笑 2024-10-31 17:11:48

*使用 C# 6.0 和朋友们,大括号不再只是用于 string.Format 了!现在它们可以表示插值字符串,您可以在其中混合 C# 对象和代码,而无需使用所有 string.Format & {0} {1} {2} 开销。

注意:插值字符串以美元符号开头$

来自上面链接的语言参考页面

用于构造字符串。插值字符串看起来像
包含插值表达式的模板字符串。一个
插值字符串返回一个替换插值字符串的字符串
它包含的表达式及其字符串表示形式。

内插字符串的参数比
复合格式字符串。例如,插值字符串

Console.WriteLine($"名称 = {name},小时 = {小时:hh}");

包含两个内插表达式“{name}”和“{hours:hh}”。这
等效的复合格式字符串是:

Console.WriteLine("名称 = {0},小时 = {1:hh}",名称,小时);

注意: 如果您没有不知道,Console.WriteLine 有一种内置的 string.Format,如果您没有意识到在上面的示例中可能并不明显如果

您想在不依赖 Console.WriteLine 魔法的情况下获取相同的字符串,则可能更容易理解这...

string message = $"Name = {name}, hours = {hours:hh}"; // interpolated

...相当于...

string message = string.Format("Name = {0}, hours = {1:hh}", name, hours); // old school

内插字符串的结构是:

$"<文本> {<插值表达式> [,<字段宽度>] [<:格式字符串>] } <文本> ..."
其中:

  • field-width 是一个有符号整数,表示字段中的字符数。如果为正,则该字段右对齐;如果为负,则左对齐。
  • format-string 是适合正在格式化的对象类型的格式字符串。例如,对于 DateTime 值,它可以是标准日期和时间格式字符串,例如“D”或“d”。

您可以在任何可以使用字符串的地方使用内插字符串
文字。每次使用代码时都会评估内插字符串
执行内插字符串。这允许您分离
内插字符串的定义和评估。

要在内插字符串中包含大括号(“{”或“}”),请使用
两个大括号“{{”或“}}”。


* 正如@Ben 在上面的评论中指出的。 (抱歉,在路上错过了。)

*With C# 6.0 & friends, curly braces aren't just for string.Format any more! Now they can denote interpolated strings, where you can mix in C# objects and code without all the string.Format & {0} {1} {2} overhead.

NOTE: Interpolated strings start with a dollar sign: $

From the language reference page linked above:

Used to construct strings. An interpolated string looks like a
template string that contains interpolated expressions. An
interpolated string returns a string that replaces the interpolated
expressions that it contains with their string representations.

The arguments of an interpolated string are easier to understand than
a composite format string. For example, the interpolated string

Console.WriteLine($"Name = {name}, hours = {hours:hh}");

contains two interpolated expressions, '{name}' and '{hours:hh}'. The
equivalent composite format string is:

Console.WriteLine("Name = {0}, hours = {1:hh}", name, hours);

Note: If you didn't know, Console.WriteLine has a sort of built-in string.Format, which might not be obvious in the above example if you didn't realize that going in.

If you wanted to get the same string out without relying on Console.WriteLine magic, it might be easier to read that this...

string message = $"Name = {name}, hours = {hours:hh}"; // interpolated

... is equivalent to...

string message = string.Format("Name = {0}, hours = {1:hh}", name, hours); // old school

The structure of an interpolated string is:

$"<text> {<interpolated-expression> [,<field-width>] [<:format-string>] } <text> ..."
where:

  • field-width is a signed integer that indicates the number of characters in the field. If it is positive, the field is right-aligned; if negative, left-aligned.
  • format-string is a format string appropriate for the type of object being formatted. For example, for a DateTime value, it could be a standard date and time format string such as "D" or "d".

You can use an interpolated string anywhere you can use a string
literal. The interpolated string is evaluated each time the code with
the interpolated string executes. This allows you to separate the
definition and evaluation of an interpolated string.

To include a curly brace ("{" or "}") in an interpolated string, use
two curly braces, "{{" or "}}".


* As @Ben points out in a comment, above. (Sorry, missed that on the way in.)

冰之心 2024-10-31 17:11:48

几乎可以肯定它稍后会在 String.Format 调用中使用,其中编号占位符被附加参数替换:

string something = "I have {0} cats";
int myNumCats = 2
var theResult = String.Format(something,myNumCats);

Its almost certainly later used in a String.Format call, where the numbered placeholders are replaced by additional parameters:

string something = "I have {0} cats";
int myNumCats = 2
var theResult = String.Format(something,myNumCats);
输什么也不输骨气 2024-10-31 17:11:48

检查 http://msdn.microsoft.com/ es-es/library/b1csw23d%28v=vs.80%29.aspx,它是string.format方法的文档,它是用于替换{0} 具有值。

Check http://msdn.microsoft.com/es-es/library/b1csw23d%28v=vs.80%29.aspx, its the documentation for the string.format method, it is the method used for replacing the {0} with a value.

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