简体   繁体   中英

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 .

for example String.Format("{0:X}", 10) will return "A" , not "10"

X2 will add zeros to the left, if your hexadecimal representation is less than two symbols

for example String.Format("{0:X2}", 10) will return "0A" , not "A"

0,2 will add spaces to the left, if the resulting number of symbols is less than 2.

for example String.Format("{0,3:X2}", 10) will return " 0A" , but not "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. 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.

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.

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. 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
  2. X2 - Formats a number 10 into hexadecimel value 0A .

Update

Code

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

Result: 0A

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

Conclusion from me
You can change "{0,2:X2}" to "{0:X2}" , live example here .

Reference Links: MSDN

According to MSDN , a format string has the following format:

{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.
,2 is the alignment part, if the result is shorter than that, it is padded left with spaces.
:X2 is the formatString part. 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.

In this specific case the alignment specifier is redundant, because X2 already specifies a minimum width of 2.

See here for more info on the format string:
Composite Formatting
Standard Numeric Format Strings

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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