简体   繁体   English

c#:字符串格式

[英]c#: string format

I came across a code. 我遇到了一个代码。 Can anybody shed a bit of light on it. 任何人都可以对此有所了解。 Plz be kind if anybody finds it a bit basic. 如果有人发现它有点基本,那就好。

string str= String.Format("{0,2:X2}", (int)value);

Thankyou for your time. 感谢您的时间。

X format returns Hexadecimal representation of your value . X格式返回value 十六进制表示。

for example String.Format("{0:X}", 10) will return "A" , not "10" 例如String.Format("{0:X}", 10)将返回"A" ,而不是"10"

X2 will add zeros to the left, if your hexadecimal representation is less than two symbols 如果您的十六进制表示少于两个符号, X2将在左侧添加

for example String.Format("{0:X2}", 10) will return "0A" , not "A" 例如String.Format("{0:X2}", 10)将返回"0A" ,而不是"A"

0,2 will add spaces to the left, if the resulting number of symbols is less than 2. 0,2将添加空格的左侧,如果所得到的符号数是小于2。

for example String.Format("{0,3:X2}", 10) will return " 0A" , but not "0A" 例如String.Format("{0,3:X2}", 10)将返回" 0A" ,但不返回"0A"

So as result this format {0,2:X2} will return your value in Hexadecimal notation appended by one zero from the left if it is only one symbol and then appended by space from the left if is it one symbol. 因此,如果结果只有一个符号,那么这个格式{0,2:X2}将以十六进制表示法的形式返回你的值,如果它只是一个符号则从左边附加一个零,如果它是一个符号则从左边附加空格。 After reading this several times, you can see, that ,2 is redundant and this format can be simplified to {0:X2} without changing the behavior. 在多次阅读之后,您可以看到, ,2是冗余的,这种格式可以简化为{0:X2}而不改变行为。

Some notes: 一些说明:

: separates indexing number and specific format applied to that object . :分隔应用于该对象的索引编号和特定格式。 For example this code 例如这段代码

String.Format("{0:X} {1:N} {0:N}", 10, 20)

shows, that I want to format 10 (index 0) in hexadecimal then show 20 (index 1) in numerical way, and then also format 10 (index 0) in numeric way. 显示,我想以十六进制格式化10 (索引0),然后以数字方式显示20 (索引1),然后以数字方式格式化10 (索引0)。

0,2 from the left part of semi-column indicated index position 0 and format ,2 applied to the resulting string , not to a specific object. 0,2从半列的左侧部分指示索引位置0和格式,2应用于结果字符串 ,而不是特定对象。 So this code 所以这段代码

String.Format("{0,1} {1,2} {0,4}", 10, 20)

will print first number with at least one symbol, second with at least two symbols and then again first number with at least four symbols occupied. 将打印带有至少一个符号的第一个数字,第二个带有至少两个符号,然后再打印第一个带有至少四个符号的数字。 If the number of symbols in resulting string will be less - they will be populated by spaces. 如果结果字符串中的符号数量较少 - 它们将由空格填充。

{0,2:X2}

It splits into 它分裂成

  1. 0,2 - Format a number 10 into 10 0,2 - 将数字10格式化为10
  2. X2 - Formats a number 10 into hexadecimel value 0A . X2 - 将数字10格式化为十六进制值0A

Update 更新

Code

String.Format("{0,2:X2}", (int)value); // where value = 10

Result: 0A 结果: 0A

Live Example: http://ideone.com/NW0U26 实例: http//ideone.com/NW0U26

Conclusion from me 我的结论
You can change "{0,2:X2}" to "{0:X2}" , live example here . 您可以将"{0,2:X2}"更改为"{0:X2}"此处为实例

Reference Links: MSDN 参考链接: MSDN

According to MSDN , a format string has the following format: 根据MSDN ,格式字符串具有以下格式:

{index[,alignment][:formatString]}

We can find all of these components (the last two being optional) in your format string: 我们可以在您的格式字符串中找到所有这些组件(最后两个是可选的):

0 is the index of the parameter to use. 0是要使用的参数的索引
,2 is the alignment part, if the result is shorter than that, it is padded left with spaces. ,2对齐部分,如果结果比这短,则用空格填充左边。
:X2 is the formatString part. :X2formatString部分。 It means the number will be formatted in hexadecimal (uppercase) format, with a minimum width of 2. If the resulting number has less than 2 digits, it is padded with zeroes on the left. 这意味着数字将以十六进制(大写)格式格式化,最小宽度为2.如果结果数字少于2位,则在左侧用零填充。

In this specific case the alignment specifier is redundant, because X2 already specifies a minimum width of 2. 在这种特定情况下,对齐说明符是多余的,因为X2已经指定了最小宽度2。

See here for more info on the format string: 有关格式字符串的更多信息,请参见此处:
Composite Formatting 复合格式
Standard Numeric Format Strings 标准数字格式字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM