C# String.Format 指定字符串宽度和对齐方式
2011-09-02 23:01
489 查看
String.Format("{0,-10}",str);//这个表示第一个参数str字符串的宽度为10,左对齐
String.Format("{0,10}",str);//这个表示第一个参数str字符串的宽度为10,右对齐
参考:From MSDN
components:
{index[,alignment][:formatString]}
The matching braces ("{" and "}") are required.
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.
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.
appropriate for the type of object being formatted. Specify a if the corresponding object is a numeric value, a
if the corresponding object is a object, or an
if the corrersponding 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.
String.Format("{0,10}",str);//这个表示第一个参数str字符串的宽度为10,右对齐
参考:From MSDN
Format Item Syntax
Each format item takes the following form and consists of the followingcomponents:
{index[,alignment][:formatString]}
The matching braces ("{" and "}") are required.
Index Component
The mandatory index component, also called a parameter specifier, is anumber 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 thepreferred 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 isappropriate for the type of object being formatted. Specify a if the corresponding object is a numeric value, a
if the corresponding object is a object, or an
if the corrersponding 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.
相关文章推荐
- C# String.Format 指定字符串宽度和对齐方式
- C# String.Format 指定字符串宽度和对齐方式
- C# string.format设置字符串宽度 对齐
- C# 格式化字符串几种方式总结 String.Format
- string.Format 指定字符串宽度
- C# 格式化字符串几种方式总结 String.Format
- c# 字符串连接使用“+”和string.format格式化两种方式
- C# 格式化字符串几种方式总结 String.Format
- 【整理】C# ToString格式字符串整理(Format)(数字、日期和枚举的标准格式设置说明符)(SamWang)
- 扩展js,实现c#中的string.format方便拼接字符串
- C# string.Format字符串格式化
- c# ToString() 格式化字符串 、C# String.Format格式说明
- C# 格式化字符串 String.Format
- string.Format对C#字符串格式化
- C# 格式化字符串 String.Format (推荐)
- string.Format对C#字符串格式化
- string.Format对C#字符串格式化
- MessageFormat对象实现格式化字符串输出,类似C#中的string.format方法
- .NET C# Tostring() format 格式化字符串大全
- string.Format对C#字符串格式化