简体   繁体   English

这种C#字符串格式是什么意思?

[英]What does this C# string format mean?

From my previous question, Converting chinese character to Unicode , I had a good answer but with some code I didn't understand: 在我之前的问题( 将汉字转换为Unicode)中 ,我得到了一个很好的答案,但是使用了一些我不理解的代码:

Console.WriteLine("U+{0:x4}", (int)myChar);

Could anyone explain this? 有人可以解释吗?

Console.WriteLine("U+{0:x4}", (int)myChar);

is the equivalent to the call: 等价于通话:

Console.WriteLine("U+{0}", ((int)myChar).ToString("x4"));

In a format string, the : indicates that the item should be displayed using the provided format. 在格式字符串中, :表示应使用提供的格式显示该项目。 The x4 part indicates that the integer should be printed in its hexadecimal form using 4 characters. x4部分表示该整数应使用4字符以十六进制形式打印。 Refer to standard numeric format strings for more information. 有关更多信息,请参考标准数字格式字符串

The 0 indicates which positional argument to substitute. 0表示要替换的位置参数。 The x displays a hexadecimal number, the 4 has it display four digits. x显示一个十六进制数字, 4显示四个数字。

For example, the character ȿ (LATIN SMALL LETTER S WITH SWASH TAIL, codepoint 575) is printed as U+023F since 575 10 = 23F 16 . 例如,由于575 10 = 23F 16 ,字符ȿ (带有SWASH TAIL的拉丁文小写字母 S,代码点575)被打印为U+023F

That will simply create the literal string "U+1234"... now if you are wanting to convert a unicode code point into a char, you want Convert.ToChar(myChar) 这将只创建文字字符串“ U + 1234” ...现在,如果您要将Unicode代码点转换为char,则需要Convert.ToChar(myChar)

http://msdn.microsoft.com/en-us/library/3hkfdkcx.aspx http://msdn.microsoft.com/zh-CN/library/3hkfdkcx.aspx

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

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