永发信息网

c#中string message = string.Format()的意义和用法

答案:2  悬赏:20  手机版
解决时间 2021-03-23 00:11
  • 提问者网友:ミ烙印ゝ
  • 2021-03-22 20:02
c#中string message = string.Format()的意义和用法
最佳答案
  • 五星知识达人网友:动情书生
  • 2021-03-22 20:30
这是格式字符串。前面的{}里的数字是索引,而且必须从0开始。引号后面的值的个数必须和索引一样,而且必须一一对应。这个一般是在输出字符串里面包含有变量的时候用。
全部回答
  • 1楼网友:你可爱的野爹
  • 2021-03-22 22:04
参数
format
类型:System..::.String

复合格式字符串。

args
类型:array[]()[]

包含零个或多个要格式化的对象的 Object 数组。

返回值
类型:System..::.String

format 的一个副本,其中格式项已替换为 args 中相应 Object 实例的 String 等效项。

异常
异常 条件
ArgumentNullException format 或 args 为 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing)。

FormatException format 无效。

- 或 -

用于指示要格式化的参数的数字小于零,或者大于等于 args 数组的长度。

备注
此方法使用 .NET Framework 的复合格式设置功能将对象的值转换为其文本表示形式,并将该表示形式嵌入字符串中。.NET Framework 提供了广泛的格式设置支持,下面的格式设置主题中对此有更详细的描述。

有关 Format、AppendFormat 等方法以及 WriteLine 的某些重载所支持的复合格式设置功能的更多信息,请参见复合格式设置。

有关数值格式说明符的更多信息,请参见标准数字格式字符串和自定义数字格式字符串。

有关日期和时间格式说明符的更多信息,请参见标准日期和时间格式字符串和自定义日期和时间格式字符串。

有关枚举格式说明符的更多信息,请参见枚举格式字符串。

有关格式设置的更多信息,请参见为类型设置格式和格式设置概述。

format 参数由零个或多个文本序列与零个或多个索引占位符混合组成,其中索引占位符称为格式项,它们与此方法的参数列表中的对象相对应。格式设置过程将每个格式项替换为相应对象值的文本表示形式。

格式项的语法是 ,它指定了一个强制索引、格式化文本的可选长度和对齐方式,以及格式说明符字符的可选字符串,其中格式说明符字符用于控制如何设置相应对象的值的格式。格式项的组成部分包括:

索引
从零开始的整数,指示对象列表中要格式化的元素。如果由索引指定的对象为 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing),则格式项将被空字符串 ("") 替换。

对齐方式
可选整数,指示包含格式化值的区域的最小宽度。如果格式化值的长度小于对齐方式,则用空格填充该区域。如果对齐方式为负,则格式化值将在该区域中左对齐;如果对齐方式为正,则格式化值将右对齐。如果没有指定对齐方式,则该区域的长度为格式化值的长度。如果指定对齐方式,则需要使用逗号。

格式字符串
可选的格式说明符字符串。如果没有指定格式字符串,并且对应的参数实现了 IFormattable 接口,则将 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing) 用作 IFormattable..::.ToString 格式字符串。因此,IFormattable..::.ToString 的所有实现都必须允许 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing) 作为格式字符串,并以 String 对象的形式返回对象表示形式的默认格式设置。如果指定格式字符串,则需要使用冒号。

必须使用前导大括号字符和尾部大括号字符,即“”。若要在 format 中指定单个大括号字符,请指定两个前导大括号字符或尾部大括号字符;即“}”。

如果 format 的值为“Thank you for your purchase of copies of Microsoft®.NET (Core Reference).”,并且 arg[0] 是值为 123 的 Int16,则返回值为:

“Thank you for your purchase of 123 copies of Microsoft®.NET (Core Reference).”

如果 format 的值为“Brad's dog has fleas.”,arg[0] 是值为 42 的 Int16(在此示例中,下划线表示填充空格),则返回值为:

“Brad's dog has 42______ fleas.”

示例
下面的代码示例演示数字、日期和枚举的标准格式设置说明符。

Visual Basic 复制代码
' This code example demonstrates the String.Format() method.
' This example uses the provider parameter to supply formatting
' information using the invariant culture.

Imports System.Globalization

Class Sample
Public Enum Color
Yellow = 1
Blue = 2
Green = 3
End Enum 'Color

Private Shared thisDate As DateTime = DateTime.Now

Public Shared Sub Main()

' Store the output of the String.Format method in a string.
Dim s As String = ""

Console.Clear()

' Format a negative integer or floating-point number in various ways.
Console.WriteLine("Standard Numeric Format Specifiers")
s = String.Format(CultureInfo.InvariantCulture, _
"(C) Currency: . . . . . . . . " & vbCrLf & _
"(D) Decimal:. . . . . . . . . " & vbCrLf & _
"(E) Scientific: . . . . . . . " & vbCrLf & _
"(F) Fixed point:. . . . . . . " & vbCrLf & _
"(G) General:. . . . . . . . . " & vbCrLf & _
" (default):. . . . . . . . (default = 'G')" & vbCrLf & _
"(N) Number: . . . . . . . . . " & vbCrLf & _
"(P) Percent:. . . . . . . . . " & vbCrLf & _
"(R) Round-trip: . . . . . . . " & vbCrLf & _
"(X) Hexadecimal:. . . . . . . " & vbCrLf, _
- 123, - 123.45F)
Console.WriteLine(s)

' Format the current date in various ways.
Console.WriteLine("Standard DateTime Format Specifiers")
s = String.Format(CultureInfo.InvariantCulture.DateTimeFormat, _
"(d) Short date: . . . . . . . " & vbCrLf & _
"(D) Long date:. . . . . . . . " & vbCrLf & _
"(t) Short time: . . . . . . . " & vbCrLf & _
"(T) Long time:. . . . . . . . " & vbCrLf & _
"(f) Full date/short time: . . " & vbCrLf & _
"(F) Full date/long time:. . . " & vbCrLf & _
"(g) General date/short time:. " & vbCrLf & _
"(G) General date/long time: . " & vbCrLf & _
" (default):. . . . . . . . (default = 'G')" & vbCrLf & _
"(M) Month:. . . . . . . . . . " & vbCrLf & _
"(R) RFC1123:. . . . . . . . . " & vbCrLf & _
"(s) Sortable: . . . . . . . . " & vbCrLf & _
"(u) Universal sortable: . . . (invariant)" & vbCrLf & _
"(U) Universal sortable: . . . " & vbCrLf & _
"(Y) Year: . . . . . . . . . . " & vbCrLf, _
thisDate)
Console.WriteLine(s)

' Format a Color enumeration value in various ways.
Console.WriteLine("Standard Enumeration Format Specifiers")
s = String.Format(CultureInfo.InvariantCulture, _
"(G) General:. . . . . . . . . " & vbCrLf & _
" (default):. . . . . . . . (default = 'G')" & vbCrLf & _
"(F) Flags:. . . . . . . . . . (flags or integer)" & vbCrLf & _
"(D) Decimal number: . . . . . " & vbCrLf & _
"(X) Hexadecimal:. . . . . . . " & vbCrLf, _
Color.Green)
Console.WriteLine(s)
End Sub 'Main
End Class 'Sample
'
' This example displays the following output to the console:
'
' Standard Numeric Format Specifiers
' (C) Currency: . . . . . . . . (123.00)
' (D) Decimal:. . . . . . . . . -123
' (E) Scientific: . . . . . . . -1.234500E+002
' (F) Fixed point:. . . . . . . -123.45
' (G) General:. . . . . . . . . -123
' (default):. . . . . . . . -123 (default = 'G')
' (N) Number: . . . . . . . . . -123.00
' (P) Percent:. . . . . . . . . -12,345.00 %
' (R) Round-trip: . . . . . . . -123.45
' (X) Hexadecimal:. . . . . . . FFFFFF85
'
' Standard DateTime Format Specifiers
' (d) Short date: . . . . . . . 07/09/2007
' (D) Long date:. . . . . . . . Monday, 09 July 2007
' (t) Short time: . . . . . . . 13:42
' (T) Long time:. . . . . . . . 13:42:50
' (f) Full date/short time: . . Monday, 09 July 2007 13:42
' (F) Full date/long time:. . . Monday, 09 July 2007 13:42:50
' (g) General date/short time:. 07/09/2007 13:42
' (G) General date/long time: . 07/09/2007 13:42:50
' (default):. . . . . . . . 07/09/2007 13:42:50 (default = 'G')
' (M) Month:. . . . . . . . . . July 09
' (R) RFC1123:. . . . . . . . . Mon, 09 Jul 2007 13:42:50 GMT
' (s) Sortable: . . . . . . . . 2007-07-09T13:42:50
' (u) Universal sortable: . . . 2007-07-09 13:42:50Z (invariant)
' (U) Universal sortable: . . . Monday, 09 July 2007 20:42:50
' (Y) Year: . . . . . . . . . . 2007 July
'
' Standard Enumeration Format Specifiers
' (G) General:. . . . . . . . . Green
' (default):. . . . . . . . Green (default = 'G')
' (F) Flags:. . . . . . . . . . Green (flags or integer)
' (D) Decimal number: . . . . . 3
' (X) Hexadecimal:. . . . . . . 00000003

C# 复制代码
// This code example demonstrates the String.Format() method.
// Formatting for this example uses the "en-US" culture.

using System;
using System.Globalization;

class Sample
{
enum Color ;
static DateTime thisDate = DateTime.Now;

public static void Main()
{
// Store the output of the String.Format method in a string.
string s = "";

Console.Clear();

// Format a negative integer or floating-point number in various ways.
Console.WriteLine("Standard Numeric Format Specifiers");
s = String.Format(CultureInfo.InvariantCulture,
"(C) Currency: . . . . . . . . \n" +
"(D) Decimal:. . . . . . . . . \n" +
"(E) Scientific: . . . . . . . \n" +
"(F) Fixed point:. . . . . . . \n" +
"(G) General:. . . . . . . . . \n" +
" (default):. . . . . . . . (default = 'G')\n" +
"(N) Number: . . . . . . . . . \n" +
"(P) Percent:. . . . . . . . . \n" +
"(R) Round-trip: . . . . . . . \n" +
"(X) Hexadecimal:. . . . . . . \n",
-123, -123.45f);
Console.WriteLine(s);

// Format the current date in various ways.
Console.WriteLine("Standard DateTime Format Specifiers");
s = String.Format(CultureInfo.InvariantCulture.DateTimeFormat,
"(d) Short date: . . . . . . . \n" +
"(D) Long date:. . . . . . . . \n" +
"(t) Short time: . . . . . . . \n" +
"(T) Long time:. . . . . . . . \n" +
"(f) Full date/short time: . . \n" +
"(F) Full date/long time:. . . \n" +
"(g) General date/short time:. \n" +
"(G) General date/long time: . \n" +
" (default):. . . . . . . . (default = 'G')\n" +
"(M) Month:. . . . . . . . . . \n" +
"(R) RFC1123:. . . . . . . . . \n" +
"(s) Sortable: . . . . . . . . \n" +
"(u) Universal sortable: . . . (invariant)\n" +
"(U) Universal sortable: . . . \n" +
"(Y) Year: . . . . . . . . . . \n",
thisDate);
Console.WriteLine(s);

// Format a Color enumeration value in various ways.
Console.WriteLine("Standard Enumeration Format Specifiers");
s = String.Format(CultureInfo.InvariantCulture,
"(G) General:. . . . . . . . . \n" +
" (default):. . . . . . . . (default = 'G')\n" +
"(F) Flags:. . . . . . . . . . (flags or integer)\n" +
"(D) Decimal number: . . . . . \n" +
"(X) Hexadecimal:. . . . . . . \n",
Color.Green);
Console.WriteLine(s);
}
}

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯