如何使用 string.Format() 格式化用大括号包围的十六进制数?
How to use string.Format() to format a hex number surrounded by curly brackets?
输入: uint hex = 0xdeadbeef;
要求输出: string result = "{deadbeef}"
第一种方法:显式添加 {
和 }
;这有效:
result = "{" + string.Format("{0:x}", hex) + "}"; // -> "{deadbeef}"
使用转义大括号输出为十进制而不是十六进制:
result = string.Format("{{{0}}}", hex); // -> "{3735928559}"
看起来很有希望,现在我们需要做的就是按照上面的第一种方法添加 :x
十六进制说明符:
result = string.Format("{{{0:x}}}", hex); // -> "{x}"
哦,亲爱的,添加 ':x
使其输出 "{x}"
而不是我想要的 "{deadbeef}"
。
所以我的问题是:我必须按照第一个示例通过显式添加 {
和 }
来解决这个问题,还是有办法使用复合格式并转义卷曲括号?
另请注意,这也会影响字符串插值(毕竟)只是由编译器转换为对 string.Format()
.
的调用
(这很可能是重复的问题,但到目前为止我一直无法找到重复的...)
已编辑
我得到的最接近的是
string.Format("{{{0:x}\u200B}}",16)
似乎 }}} 被解释错误,插入零宽度 space 防止前两个 } 被扩展为转义 } 字符。
您可以使用空字符或添加括号作为参数:
uint hex = 0xdeadbeef;
string result = string.Format("{0}{1:x}{2}", "{", hex, "}");
这将根据您的需要输出 {deadbeef}
。
发生这种情况是因为要在 string.Format 中输出 } 你必须像这样转义它 }}.
但是当你输入 }}}
时,它会理解为 }} }
并输出 {x}
。这是 C# 中的设计错误,当您尝试将输出格式化为 :x
、:N
或其他格式时。
你也可以试试
uint hex = 0xdeadbeef;
string result = string.Format("{{ {1:x} }}", hex);
但这会输出带空格的 { deadbeef }
。
试试这个: 使用 2 次 String.Format
像这样的方法
String result= String.Format("{{{0}}}",String.Format("{0:x}", hex));
请参阅 http://msdn.microsoft.com/en-us/library/txafckwd(v=vs.110).aspx 中的 "Escaping Braces" - 基本上您的变通方法就是一个解决方案。
从那里开始:
The way escaped braces are interpreted can lead to unexpected results.
For example, consider the format item "{{{0:D}}}", which is intended
to display an opening brace, a numeric value formatted as a decimal
number, and a closing brace. However, the format item is actually
interpreted in the following manner:
1.The first two opening braces ("{{") are escaped and yield one opening brace.
2. The next three characters ("{0:") are interpreted as the start of a format item.
3. The next character ("D") would be interpreted as the Decimal standard
numeric format specifier, but the next two escaped braces ("}}") yield
a single brace. Because the resulting string ("D}") is not a standard
numeric format specifier, the resulting string is interpreted as a
custom format string that means display the literal string "D}".
4. The last brace ("}") is interpreted as the end of the format item.
5. The final result that is displayed is the literal string, "{D}". The
numeric value that was to be formatted is not displayed.
作为解决方案,根据您的示例进行了调整:
uint hex = 0xdeadbeef;
string output = string.Format("{0}{1:x}{2}",
"{", hex, "}");
Console.WriteLine(output);
输入: uint hex = 0xdeadbeef;
要求输出: string result = "{deadbeef}"
第一种方法:显式添加 {
和 }
;这有效:
result = "{" + string.Format("{0:x}", hex) + "}"; // -> "{deadbeef}"
使用转义大括号输出为十进制而不是十六进制:
result = string.Format("{{{0}}}", hex); // -> "{3735928559}"
看起来很有希望,现在我们需要做的就是按照上面的第一种方法添加 :x
十六进制说明符:
result = string.Format("{{{0:x}}}", hex); // -> "{x}"
哦,亲爱的,添加 ':x
使其输出 "{x}"
而不是我想要的 "{deadbeef}"
。
所以我的问题是:我必须按照第一个示例通过显式添加 {
和 }
来解决这个问题,还是有办法使用复合格式并转义卷曲括号?
另请注意,这也会影响字符串插值(毕竟)只是由编译器转换为对 string.Format()
.
(这很可能是重复的问题,但到目前为止我一直无法找到重复的...)
已编辑
我得到的最接近的是
string.Format("{{{0:x}\u200B}}",16)
似乎 }}} 被解释错误,插入零宽度 space 防止前两个 } 被扩展为转义 } 字符。
您可以使用空字符或添加括号作为参数:
uint hex = 0xdeadbeef;
string result = string.Format("{0}{1:x}{2}", "{", hex, "}");
这将根据您的需要输出 {deadbeef}
。
发生这种情况是因为要在 string.Format 中输出 } 你必须像这样转义它 }}.
但是当你输入 }}}
时,它会理解为 }} }
并输出 {x}
。这是 C# 中的设计错误,当您尝试将输出格式化为 :x
、:N
或其他格式时。
你也可以试试
uint hex = 0xdeadbeef;
string result = string.Format("{{ {1:x} }}", hex);
但这会输出带空格的 { deadbeef }
。
试试这个: 使用 2 次 String.Format
像这样的方法
String result= String.Format("{{{0}}}",String.Format("{0:x}", hex));
请参阅 http://msdn.microsoft.com/en-us/library/txafckwd(v=vs.110).aspx 中的 "Escaping Braces" - 基本上您的变通方法就是一个解决方案。
从那里开始:
The way escaped braces are interpreted can lead to unexpected results. For example, consider the format item "{{{0:D}}}", which is intended to display an opening brace, a numeric value formatted as a decimal number, and a closing brace. However, the format item is actually interpreted in the following manner:
1.The first two opening braces ("{{") are escaped and yield one opening brace.
2. The next three characters ("{0:") are interpreted as the start of a format item.
3. The next character ("D") would be interpreted as the Decimal standard numeric format specifier, but the next two escaped braces ("}}") yield a single brace. Because the resulting string ("D}") is not a standard numeric format specifier, the resulting string is interpreted as a custom format string that means display the literal string "D}".
4. The last brace ("}") is interpreted as the end of the format item.
5. The final result that is displayed is the literal string, "{D}". The numeric value that was to be formatted is not displayed.
作为解决方案,根据您的示例进行了调整:
uint hex = 0xdeadbeef;
string output = string.Format("{0}{1:x}{2}",
"{", hex, "}");
Console.WriteLine(output);