c# - 十六进制的 String.Format

使用下面的代码,着色总是给出#DDDD。绿色、红色和空格值如何解决?

string colorstring;
int Blue = 13;
int Green = 0;
int Red = 0;
int Space = 14;
colorstring = String.Format("#{0:X}{0:X}{0:X}{0:X}", Blue, Green, Red, Space);

最佳答案

{0:X} 中的数字0 指的是列表或参数中的位置。在这种情况下,0 表示使用第一个值,即 Blue。使用 {1:X} 作为第二个参数(Green),依此类推。

colorstring = String.Format("#{0:X}{1:X}{2:X}{3:X}", Blue, Green, Red, Space);

格式参数的语法在 documentation 中描述。 :

Format Item Syntax

Each format item takes the following form and consists of the following components:

{ index[,alignment][:formatString]}

The matching braces ("{" and "}") are required.

Index Component

The mandatory index component, also called a parameter specifier, is a number starting from 0 that identifies a corresponding item in the list of objects. That is, the format item whose parameter specifier is 0 formats the first object in the list, the format item whose parameter specifier is 1 formats the second object in the list, and so on.

Multiple format items can refer to the same element in the list of objects by specifying the same parameter specifier. For example, you can format the same numeric value in hexadecimal, scientific, and number format by specifying a composite format string like this: "{0:X} {0:E} {0:N}".

Each format item can refer to any object in the list. For example, if there are three objects, you can format the second, first, and third object by specifying a composite format string like this: "{1} {0} {2}". An object that is not referenced by a format item is ignored. A runtime exception results if a parameter specifier designates an item outside the bounds of the list of objects.

Alignment Component

The optional alignment component is a signed integer indicating the preferred formatted field width. If the value of alignment is less than the length of the formatted string, alignment is ignored and the length of the formatted string is used as the field width. The formatted data in the field is right-aligned if alignment is positive and left-aligned if alignment is negative. If padding is necessary, white space is used. The comma is required if alignment is specified.

Format String Component

The optional formatString component is a format string that is appropriate for the type of object being formatted. Specify a standard or custom numeric format string if the corresponding object is a numeric value, a standard or custom date and time format string if the corresponding object is a DateTime object, or an enumeration format string if the corresponding object is an enumeration value. If formatString is not specified, the general ("G") format specifier for a numeric, date and time, or enumeration type is used. The colon is required if formatString is specified.

请注意,在您的情况下,您只有索引和格式字符串。您尚未指定(也不需要)对齐组件。

https://stackoverflow.com/questions/11618387/

相关文章:

formatting - 如何可视化或格式化差异/补丁文件?

javascript - 如何在javascript中将整数格式化为特定长度?

c# - 在 C# 中使用自定义千位分隔符

c# - 如何停止 ReSharper 删除对象初始化程序中的空格

c# - 格式化 TimeSpan 大于 24 小时

formatting - 如何关闭 JSLint 缩进警告?

excel - (Excel) 基于相邻单元格值的条件格式

html - 格式化 HTML 表格单元格以便 Excel 格式化为文本?

asp.net - 如何更改 .ASPX 自动格式化设置 (Visual Studio)

ruby - CSV.read 第 x 行的非法引用