Console.Write 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將指定之值的文字表示寫入標準輸出資料流。
多載
Write(String, Object, Object, Object, Object) |
使用指定的格式資訊,將指定之物件和可變長度參數清單的文字表示寫入標準輸出資料流。 |
Write(String, Object, Object, Object) |
使用指定的格式資訊,將指定之物件的文字表示寫入標準輸出資料流。 |
Write(Char[], Int32, Int32) |
將指定的 Unicode 字元子陣列寫入標準輸出資料流。 |
Write(String, ReadOnlySpan<Object>) | |
Write(String, Object[]) |
使用指定的格式資訊,將指定之物件陣列的文字表示寫入標準輸出資料流。 |
Write(String, Object) |
使用指定的格式資訊,將指定之物件的文字表示寫入標準輸出資料流。 |
Write(UInt64) |
將指定 64 位元不帶正負號整數值的文字表示寫入標準輸出資料流。 |
Write(UInt32) |
將指定 32 位元不帶正負號整數值的文字表示寫入標準輸出資料流。 |
Write(String) |
將指定的字串值寫入標準輸出資料流。 |
Write(String, Object, Object) |
使用指定的格式資訊,將指定之物件的文字表示寫入標準輸出資料流。 |
Write(Object) |
將指定物件的文字表示寫入標準輸出資料流。 |
Write(Single) |
將指定的單精確度浮點數值的文字表示寫入標準輸出資料流。 |
Write(Char) |
將指定的 Unicode 字元值寫入標準輸出資料流。 |
Write(Char[]) |
將指定的 Unicode 字元陣列寫入標準輸出資料流。 |
Write(Boolean) |
將指定布林值 (Boolean) 的文字表示寫入標準輸出資料流。 |
Write(Double) |
將指定的雙精確度浮點數值的文字表示寫入標準輸出資料流。 |
Write(Int32) |
將指定 32 位元帶正負號整數值的文字表示寫入標準輸出資料流。 |
Write(Int64) |
將指定 64 位元帶正負號整數值的文字表示寫入標準輸出資料流。 |
Write(Decimal) |
將指定 Decimal 值的表示文字寫入標準輸出資料流。 |
Write(String, Object, Object, Object, Object)
重要
此 API 不符合 CLS 規範。
使用指定的格式資訊,將指定之物件和可變長度參數清單的文字表示寫入標準輸出資料流。
public:
static void Write(System::String ^ format, System::Object ^ arg0, System::Object ^ arg1, System::Object ^ arg2, System::Object ^ arg3);
[System.CLSCompliant(false)]
public static void Write (string format, object arg0, object arg1, object arg2, object arg3);
[<System.CLSCompliant(false)>]
static member Write : string * obj * obj * obj * obj -> unit
Public Shared Sub Write (format As String, arg0 As Object, arg1 As Object, arg2 As Object, arg3 As Object)
參數
- format
- String
複合格式字串。
- arg0
- Object
第一個物件,使用 format
寫入。
- arg1
- Object
第二個物件,使用 format
寫入。
- arg2
- Object
第三個物件,使用 format
寫入。
- arg3
- Object
第四個物件,使用 format
寫入。
- 屬性
例外狀況
發生 I/O 錯誤。
format
為 null
。
format
中的格式規格無效。
範例
下列範例說明如何搭配 Write(String, Object, Object, Object, Object) 方法使用變數參數。 使用複合格式字串和五個格式專案呼叫 方法。
using namespace System;
//void main(array<System::String ^> ^args)
void main()
{
DateTime dateRecorded(2009, 6, 15);
DateTime startTime(1, 1, 1, 0, 30, 0);
TimeSpan interval(12, 0, 0);
Double temperature1 = 52.8;
Double temperature2 = 63.5;
Console::Write("Date: {0:d}:\n Temperature at {1:t}: {2}\n Temperature at {3:t}: {4}\n",
dateRecorded, startTime, temperature1,
startTime.Add(interval), temperature2);
Console::ReadLine();
}
// The example displays the following output:
// Date: 6/15/2009:
// Temperature at 12:30 AM: 52.8
// Temperature at 12:30 PM: 63.5
備註
注意
此應用程式開發介面不符合 CLS 標準。 符合 CLS 標準的替代項目為 Console.Write(String, Object[])。 C# 和 Visual Basic 編譯程式會自動將這個方法的呼叫解析為 的 Console.Write(String, Object[])呼叫。
這個方法會使用 .NET 的 複合格式功能 ,將物件的值轉換成其文字表示,並將該表示內嵌在字串中。 產生的字串會寫入輸出數據流。
參數 format
是由零或多個文字執行所組成,這些文字與零個或多個索引佔位元混合,稱為格式項目,對應至此方法參數清單中的物件。 格式設定程式會將每個格式專案取代為對應物件值的文字表示。
格式項目的語法是 {
index[,
alignment][:
formatString]}
,它會指定強制索引、格式化文字的選擇性長度和對齊方式,以及控制對應物件值格式化方式之格式規範字元的選擇性字符串。
.NET 提供廣泛的格式支援,如下列格式主題中更詳細地說明。
如需、 AppendFormat等方法Format所支援之複合格式功能的詳細資訊,以及的WriteLine一些多載,請參閱複合格式設定。
如需日期和時間格式規範的詳細資訊,請參閱 標準日期和時間格式字元串 和 自定義日期和時間格式字串。
如需列舉格式規範的詳細資訊,請參閱 列舉格式字串。
如需格式化的詳細資訊,請參閱 格式化類型。
給呼叫者的注意事項
這個方法會以 vararg
關鍵詞標示,這表示它支援可變數目的參數。 您可以從 Visual C++ 呼叫 方法,但無法從 C# 或 Visual Basic 程式代碼呼叫此方法。 C# 和 Visual Basic 編譯程式會將 呼叫 Write(String, Object, Object, Object, Object) 解析為 的 Write(String, Object[])呼叫。
另請參閱
適用於
Write(String, Object, Object, Object)
- 來源:
- Console.cs
- 來源:
- Console.cs
- 來源:
- Console.cs
使用指定的格式資訊,將指定之物件的文字表示寫入標準輸出資料流。
public:
static void Write(System::String ^ format, System::Object ^ arg0, System::Object ^ arg1, System::Object ^ arg2);
public static void Write (string format, object? arg0, object? arg1, object? arg2);
public static void Write (string format, object arg0, object arg1, object arg2);
static member Write : string * obj * obj * obj -> unit
Public Shared Sub Write (format As String, arg0 As Object, arg1 As Object, arg2 As Object)
參數
- format
- String
複合格式字串。
- arg0
- Object
第一個物件,使用 format
寫入。
- arg1
- Object
第二個物件,使用 format
寫入。
- arg2
- Object
第三個物件,使用 format
寫入。
例外狀況
發生 I/O 錯誤。
format
為 null
。
format
中的格式規格無效。
範例
下列範例會使用 WriteLine
方法,示範數字、日期和列舉型別的標準格式化規範。
// This code example demonstrates the Console.WriteLine() method.
// Formatting for this example uses the "en-US" culture.
using namespace System;
public enum class Color {Yellow = 1, Blue, Green};
int main()
{
DateTime thisDate = DateTime::Now;
Console::Clear();
// Format a negative integer or floating-point number in various ways.
Console::WriteLine("Standard Numeric Format Specifiers");
Console::WriteLine(
"(C) Currency: . . . . . . . . {0:C}\n" +
"(D) Decimal:. . . . . . . . . {0:D}\n" +
"(E) Scientific: . . . . . . . {1:E}\n" +
"(F) Fixed point:. . . . . . . {1:F}\n" +
"(G) General:. . . . . . . . . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(N) Number: . . . . . . . . . {0:N}\n" +
"(P) Percent:. . . . . . . . . {1:P}\n" +
"(R) Round-trip: . . . . . . . {1:R}\n" +
"(X) Hexadecimal:. . . . . . . {0:X}\n",
-123, -123.45f);
// Format the current date in various ways.
Console::WriteLine("Standard DateTime Format Specifiers");
Console::WriteLine(
"(d) Short date: . . . . . . . {0:d}\n" +
"(D) Long date:. . . . . . . . {0:D}\n" +
"(t) Short time: . . . . . . . {0:t}\n" +
"(T) Long time:. . . . . . . . {0:T}\n" +
"(f) Full date/short time: . . {0:f}\n" +
"(F) Full date/long time:. . . {0:F}\n" +
"(g) General date/short time:. {0:g}\n" +
"(G) General date/long time: . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(M) Month:. . . . . . . . . . {0:M}\n" +
"(R) RFC1123:. . . . . . . . . {0:R}\n" +
"(s) Sortable: . . . . . . . . {0:s}\n" +
"(u) Universal sortable: . . . {0:u} (invariant)\n" +
"(U) Universal full date/time: {0:U}\n" +
"(Y) Year: . . . . . . . . . . {0:Y}\n",
thisDate);
// Format a Color enumeration value in various ways.
Console::WriteLine("Standard Enumeration Format Specifiers");
Console::WriteLine(
"(G) General:. . . . . . . . . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\n" +
"(D) Decimal number: . . . . . {0:D}\n" +
"(X) Hexadecimal:. . . . . . . {0:X}\n",
Color::Green);
};
/*
This code example produces the following results:
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: . . . . . . . 6/26/2004
(D) Long date:. . . . . . . . Saturday, June 26, 2004
(t) Short time: . . . . . . . 8:11 PM
(T) Long time:. . . . . . . . 8:11:04 PM
(f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM
(F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM
(g) General date/short time:. 6/26/2004 8:11 PM
(G) General date/long time: . 6/26/2004 8:11:04 PM
(default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G')
(M) Month:. . . . . . . . . . June 26
(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT
(s) Sortable: . . . . . . . . 2004-06-26T20:11:04
(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)
(U) Universal full date/time: Sunday, June 27, 2004 3:11:04 AM
(Y) Year: . . . . . . . . . . June, 2004
Standard Enumeration Format Specifiers
(G) General:. . . . . . . . . Green
(default):. . . . . . . . Green (default = 'G')
(F) Flags:. . . . . . . . . . Green (flags or integer)
(D) Decimal number: . . . . . 3
(X) Hexadecimal:. . . . . . . 00000003
*/
// This code example demonstrates the Console.WriteLine() method.
// Formatting for this example uses the "en-US" culture.
using System;
class Sample
{
enum Color {Yellow = 1, Blue, Green};
static DateTime thisDate = DateTime.Now;
public static void Main()
{
Console.Clear();
// Format a negative integer or floating-point number in various ways.
Console.WriteLine("Standard Numeric Format Specifiers");
Console.WriteLine(
"(C) Currency: . . . . . . . . {0:C}\n" +
"(D) Decimal:. . . . . . . . . {0:D}\n" +
"(E) Scientific: . . . . . . . {1:E}\n" +
"(F) Fixed point:. . . . . . . {1:F}\n" +
"(G) General:. . . . . . . . . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(N) Number: . . . . . . . . . {0:N}\n" +
"(P) Percent:. . . . . . . . . {1:P}\n" +
"(R) Round-trip: . . . . . . . {1:R}\n" +
"(X) Hexadecimal:. . . . . . . {0:X}\n",
-123, -123.45f);
// Format the current date in various ways.
Console.WriteLine("Standard DateTime Format Specifiers");
Console.WriteLine(
"(d) Short date: . . . . . . . {0:d}\n" +
"(D) Long date:. . . . . . . . {0:D}\n" +
"(t) Short time: . . . . . . . {0:t}\n" +
"(T) Long time:. . . . . . . . {0:T}\n" +
"(f) Full date/short time: . . {0:f}\n" +
"(F) Full date/long time:. . . {0:F}\n" +
"(g) General date/short time:. {0:g}\n" +
"(G) General date/long time: . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(M) Month:. . . . . . . . . . {0:M}\n" +
"(R) RFC1123:. . . . . . . . . {0:R}\n" +
"(s) Sortable: . . . . . . . . {0:s}\n" +
"(u) Universal sortable: . . . {0:u} (invariant)\n" +
"(U) Universal full date/time: {0:U}\n" +
"(Y) Year: . . . . . . . . . . {0:Y}\n",
thisDate);
// Format a Color enumeration value in various ways.
Console.WriteLine("Standard Enumeration Format Specifiers");
Console.WriteLine(
"(G) General:. . . . . . . . . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\n" +
"(D) Decimal number: . . . . . {0:D}\n" +
"(X) Hexadecimal:. . . . . . . {0:X}\n",
Color.Green);
}
}
/*
This code example produces the following results:
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: . . . . . . . 6/26/2004
(D) Long date:. . . . . . . . Saturday, June 26, 2004
(t) Short time: . . . . . . . 8:11 PM
(T) Long time:. . . . . . . . 8:11:04 PM
(f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM
(F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM
(g) General date/short time:. 6/26/2004 8:11 PM
(G) General date/long time: . 6/26/2004 8:11:04 PM
(default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G')
(M) Month:. . . . . . . . . . June 26
(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT
(s) Sortable: . . . . . . . . 2004-06-26T20:11:04
(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)
(U) Universal full date/time: Sunday, June 27, 2004 3:11:04 AM
(Y) Year: . . . . . . . . . . June, 2004
Standard Enumeration Format Specifiers
(G) General:. . . . . . . . . Green
(default):. . . . . . . . Green (default = 'G')
(F) Flags:. . . . . . . . . . Green (flags or integer)
(D) Decimal number: . . . . . 3
(X) Hexadecimal:. . . . . . . 00000003
*/
// This code example demonstrates the Console.WriteLine() method.
// Formatting for this example uses the "en-US" culture.
open System
type Color =
| Yellow = 1
| Blue = 2
| Green = 3
let thisDate = DateTime.Now
Console.Clear()
// Format a negative integer or floating-point number in various ways.
Console.WriteLine "Standard Numeric Format Specifiers"
Console.WriteLine(
"(C) Currency: . . . . . . . . {0:C}\n" +
"(D) Decimal:. . . . . . . . . {0:D}\n" +
"(E) Scientific: . . . . . . . {1:E}\n" +
"(F) Fixed point:. . . . . . . {1:F}\n" +
"(G) General:. . . . . . . . . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(N) Number: . . . . . . . . . {0:N}\n" +
"(P) Percent:. . . . . . . . . {1:P}\n" +
"(R) Round-trip: . . . . . . . {1:R}\n" +
"(X) Hexadecimal:. . . . . . . {0:X}\n",
-123, -123.45f)
// Format the current date in various ways.
Console.WriteLine "Standard DateTime Format Specifiers"
Console.WriteLine(
"(d) Short date: . . . . . . . {0:d}\n" +
"(D) Long date:. . . . . . . . {0:D}\n" +
"(t) Short time: . . . . . . . {0:t}\n" +
"(T) Long time:. . . . . . . . {0:T}\n" +
"(f) Full date/short time: . . {0:f}\n" +
"(F) Full date/long time:. . . {0:F}\n" +
"(g) General date/short time:. {0:g}\n" +
"(G) General date/long time: . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(M) Month:. . . . . . . . . . {0:M}\n" +
"(R) RFC1123:. . . . . . . . . {0:R}\n" +
"(s) Sortable: . . . . . . . . {0:s}\n" +
"(u) Universal sortable: . . . {0:u} (invariant)\n" +
"(U) Universal full date/time: {0:U}\n" +
"(Y) Year: . . . . . . . . . . {0:Y}\n",
thisDate)
// Format a Color enumeration value in various ways.
Console.WriteLine "Standard Enumeration Format Specifiers"
Console.WriteLine(
"(G) General:. . . . . . . . . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\n" +
"(D) Decimal number: . . . . . {0:D}\n" +
"(X) Hexadecimal:. . . . . . . {0:X}\n",
Color.Green)
// This code example produces the following results:
//
// 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: . . . . . . . 6/26/2004
// (D) Long date:. . . . . . . . Saturday, June 26, 2004
// (t) Short time: . . . . . . . 8:11 PM
// (T) Long time:. . . . . . . . 8:11:04 PM
// (f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM
// (F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM
// (g) General date/short time:. 6/26/2004 8:11 PM
// (G) General date/long time: . 6/26/2004 8:11:04 PM
// (default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G')
// (M) Month:. . . . . . . . . . June 26
// (R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT
// (s) Sortable: . . . . . . . . 2004-06-26T20:11:04
// (u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)
// (U) Universal full date/time: Sunday, June 27, 2004 3:11:04 AM
// (Y) Year: . . . . . . . . . . June, 2004
//
// Standard Enumeration Format Specifiers
// (G) General:. . . . . . . . . Green
// (default):. . . . . . . . Green (default = 'G')
// (F) Flags:. . . . . . . . . . Green (flags or integer)
// (D) Decimal number: . . . . . 3
// (X) Hexadecimal:. . . . . . . 00000003
' This code example demonstrates the Console.WriteLine() method.
' Formatting for this example uses the "en-US" culture.
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()
Console.Clear()
' Format a negative integer or floating-point number in various ways.
Console.WriteLine("Standard Numeric Format Specifiers")
Console.WriteLine("(C) Currency: . . . . . . . . {0:C}" & vbCrLf & _
"(D) Decimal:. . . . . . . . . {0:D}" & vbCrLf & _
"(E) Scientific: . . . . . . . {1:E}" & vbCrLf & _
"(F) Fixed point:. . . . . . . {1:F}" & vbCrLf & _
"(G) General:. . . . . . . . . {0:G}" & vbCrLf & _
" (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _
"(N) Number: . . . . . . . . . {0:N}" & vbCrLf & _
"(P) Percent:. . . . . . . . . {1:P}" & vbCrLf & _
"(R) Round-trip: . . . . . . . {1:R}" & vbCrLf & _
"(X) Hexadecimal:. . . . . . . {0:X}" & vbCrLf, _
- 123, - 123.45F)
' Format the current date in various ways.
Console.WriteLine("Standard DateTime Format Specifiers")
Console.WriteLine("(d) Short date: . . . . . . . {0:d}" & vbCrLf & _
"(D) Long date:. . . . . . . . {0:D}" & vbCrLf & _
"(t) Short time: . . . . . . . {0:t}" & vbCrLf & _
"(T) Long time:. . . . . . . . {0:T}" & vbCrLf & _
"(f) Full date/short time: . . {0:f}" & vbCrLf & _
"(F) Full date/long time:. . . {0:F}" & vbCrLf & _
"(g) General date/short time:. {0:g}" & vbCrLf & _
"(G) General date/long time: . {0:G}" & vbCrLf & _
" (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _
"(M) Month:. . . . . . . . . . {0:M}" & vbCrLf & _
"(R) RFC1123:. . . . . . . . . {0:R}" & vbCrLf & _
"(s) Sortable: . . . . . . . . {0:s}" & vbCrLf & _
"(u) Universal sortable: . . . {0:u} (invariant)" & vbCrLf & _
"(U) Universal full date/time: {0:U}" & vbCrLf & _
"(Y) Year: . . . . . . . . . . {0:Y}" & vbCrLf, _
thisDate)
' Format a Color enumeration value in various ways.
Console.WriteLine("Standard Enumeration Format Specifiers")
Console.WriteLine("(G) General:. . . . . . . . . {0:G}" & vbCrLf & _
" (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _
"(F) Flags:. . . . . . . . . . {0:F} (flags or integer)" & vbCrLf & _
"(D) Decimal number: . . . . . {0:D}" & vbCrLf & _
"(X) Hexadecimal:. . . . . . . {0:X}" & vbCrLf, _
Color.Green)
End Sub
End Class
'
'This code example produces the following results:
'
'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: . . . . . . . 6/26/2004
'(D) Long date:. . . . . . . . Saturday, June 26, 2004
'(t) Short time: . . . . . . . 8:11 PM
'(T) Long time:. . . . . . . . 8:11:04 PM
'(f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM
'(F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM
'(g) General date/short time:. 6/26/2004 8:11 PM
'(G) General date/long time: . 6/26/2004 8:11:04 PM
' (default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G')
'(M) Month:. . . . . . . . . . June 26
'(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT
'(s) Sortable: . . . . . . . . 2004-06-26T20:11:04
'(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)
'(U) Universal full date/time: Sunday, June 27, 2004 3:11:04 AM
'(Y) Year: . . . . . . . . . . June, 2004
'
'Standard Enumeration Format Specifiers
'(G) General:. . . . . . . . . Green
' (default):. . . . . . . . Green (default = 'G')
'(F) Flags:. . . . . . . . . . Green (flags or integer)
'(D) Decimal number: . . . . . 3
'(X) Hexadecimal:. . . . . . . 00000003
'
在下列程式碼中,說明了如何使用 Write
方法。
int main()
{
array<String^>^lineInputArr = {"1 2.2 hello TRUE","2 5.22 bye FALSE","3 6.38 see ya' TRUE"};
for ( Int32 i = 0; i < 3; i++ )
{
String^ lineInput = lineInputArr->GetValue( i )->ToString();
String^ aChar = "\t";
array<String^>^fields = lineInput->Split( aChar->ToCharArray() );
Boolean isFirstField = true;
for ( Int32 i = 0; i < fields->Length; i++ )
{
if ( isFirstField )
isFirstField = false;
else
Console::Write( "," );
// If the field represents a boolean, replace with a numeric representation.
try
{
Console::Write( Convert::ToByte( Convert::ToBoolean( fields[ i ] ) ) );
}
catch ( FormatException^ )
{
Console::Write( fields[ i ] );
}
}
Console::WriteLine();
}
}
public class FormatConverter
{
public static void Main(string[] args)
{
string lineInput;
while ((lineInput = Console.ReadLine()) != null)
{
string[] fields = lineInput.Split(new char[] {'\t'});
bool isFirstField = true;
foreach (var item in fields)
{
if (isFirstField)
isFirstField = false;
else
Console.Write(',');
// If the field represents a boolean, replace with a numeric representation.
bool itemBool;
if (Boolean.TryParse(item, out itemBool))
Console.Write(Convert.ToByte(itemBool));
else
Console.Write(item);
}
Console.WriteLine();
}
}
}
let mutable lineInput = Console.ReadLine()
while lineInput <> null do
let fields = lineInput.Split '\t'
let mutable isFirstField = true
for item in fields do
if isFirstField then
isFirstField <- false
else
Console.Write ','
// If the field represents a boolean, replace with a numeric representation.
match Boolean.TryParse item with
| true, itemBool ->
Console.Write(Convert.ToByte itemBool)
| _ ->
Console.Write item
Console.WriteLine()
Public Class FormatConverter
Public Shared Sub Main()
Dim lineInput As String = Console.ReadLine()
While Not lineInput Is Nothing
Dim fields As String() = lineInput.Split(ControlChars.Tab)
Dim isFirstField As Boolean = True
For Each item As String In fields
If isFirstField Then
isFirstField = False
Else
Console.Write(",")
End If
' If the field represents a boolean, replace with a numeric representation.
Dim itemBool As Boolean
If Boolean.TryParse(item, itemBool)
Console.Write(Convert.ToByte(itemBool))
Else
Console.Write(item)
End If
Next
Console.WriteLine()
lineInput = Console.ReadLine()
End While
End Sub
End Class
備註
這個方法會使用 .NET 的 複合格式功能 ,將物件的值轉換成其文字表示,並將該表示內嵌在字串中。 產生的字串會寫入輸出數據流。
參數 format
是由零或多個文字執行所組成,這些文字與零個或多個索引佔位元混合,稱為格式項目,對應至此方法參數清單中的物件。 格式設定程式會將每個格式專案取代為對應物件值的文字表示。
格式項目的語法是 {
index[,
alignment][:
formatString]}
,它會指定強制索引、格式化文字的選擇性長度和對齊方式,以及控制對應物件值格式化方式之格式規範字元的選擇性字符串。
.NET 提供廣泛的格式支援,如下列格式主題中更詳細地說明。
如需、 AppendFormat等方法Format所支援之複合格式功能的詳細資訊,以及的WriteLine一些多載,請參閱複合格式設定。
如需日期和時間格式規範的詳細資訊,請參閱 標準日期和時間格式字元串 和 自定義日期和時間格式字串。
如需列舉格式規範的詳細資訊,請參閱 列舉格式字串。
如需格式化的詳細資訊,請參閱 格式化類型。
另請參閱
適用於
Write(Char[], Int32, Int32)
- 來源:
- Console.cs
- 來源:
- Console.cs
- 來源:
- Console.cs
將指定的 Unicode 字元子陣列寫入標準輸出資料流。
public:
static void Write(cli::array <char> ^ buffer, int index, int count);
public static void Write (char[] buffer, int index, int count);
static member Write : char[] * int * int -> unit
Public Shared Sub Write (buffer As Char(), index As Integer, count As Integer)
參數
- buffer
- Char[]
Unicode 字元陣列。
- index
- Int32
buffer
中的開始位置。
- count
- Int32
要寫入的字元數。
例外狀況
buffer
為 null
。
index
或 count
小於零。
index
加上 count
指定的位置不在 buffer
內。
發生 I/O 錯誤。
備註
這個方法會將count
從位置index
buffer
開始的字元寫入標準輸出數據流。
另請參閱
適用於
Write(String, ReadOnlySpan<Object>)
public:
static void Write(System::String ^ format, ReadOnlySpan<System::Object ^> arg);
public static void Write (string format, scoped ReadOnlySpan<object?> arg);
static member Write : string * ReadOnlySpan<obj> -> unit
Public Shared Sub Write (format As String, arg As ReadOnlySpan(Of Object))
參數
- format
- String
- arg
- ReadOnlySpan<Object>
適用於
Write(String, Object[])
- 來源:
- Console.cs
- 來源:
- Console.cs
- 來源:
- Console.cs
使用指定的格式資訊,將指定之物件陣列的文字表示寫入標準輸出資料流。
public:
static void Write(System::String ^ format, ... cli::array <System::Object ^> ^ arg);
public static void Write (string format, params object?[]? arg);
public static void Write (string format, params object[] arg);
static member Write : string * obj[] -> unit
Public Shared Sub Write (format As String, ParamArray arg As Object())
參數
- format
- String
複合格式字串。
- arg
- Object[]
要使用 format
寫入的物件陣列。
例外狀況
發生 I/O 錯誤。
format
或 arg
為 null
。
format
中的格式規格無效。
範例
下列範例會定義一個 Person
類別,其中包含一些可提供人員相關信息的屬性。 其 GetDescription
方法會傳回陣列,其中包含除了一個以外的所有屬性值。 然後,此範例會使用 方法傳 GetDescription
回的數位來顯示 Person 物件的值。
using System;
public class Person
{
public String Name { get; set; }
public DateTime BirthDate { get; set; }
public Double Height { get; set; }
public Double Weight { get; set; }
public Char Gender { get; set; }
public String Remarks { get; set; }
public object[] GetDescription()
{
return new object[] { Name, Gender, Height, Weight, BirthDate};
}
}
public class Example
{
public static void Main()
{
var p1 = new Person() { Name = "John", Gender = 'M',
BirthDate = new DateTime(1992, 5, 10),
Height = 73.5, Weight = 207 };
p1.Remarks = "Client since 1/3/2012";
Console.Write("{0}: {1}, born {4:d} Height {2} inches, Weight {3} lbs ",
p1.GetDescription());
if (String.IsNullOrEmpty(p1.Remarks))
Console.WriteLine();
else
Console.WriteLine("{1}Remarks: {0}", p1.Remarks,
Console.CursorLeft + p1.Remarks.Length + 10 > Console.WindowWidth ?
"\n " : "");
}
}
// The example displays the following output:
// John: M, born 5/10/1992 Height 73.5 inches, Weight 207 lbs Remarks: Client since 1/3/2012
open System
type Person =
{ Name: string
BirthDate: DateOnly
Height: double
Weight: double
Gender: char
Remarks: string }
member this.GetDescription(): obj [] =
[| this.Name; this.Gender; this.Height; this.Weight; this.BirthDate |]
let p1 =
{ Name = "John"
Gender = 'M'
BirthDate = DateOnly(1992, 5, 10)
Height = 73.5
Weight = 207
Remarks = "Client since 1/3/2012" }
Console.Write("{0}: {1}, born {4:d} Height {2} inches, Weight {3} lbs ", p1.GetDescription())
if String.IsNullOrEmpty p1.Remarks then
Console.WriteLine()
else
Console.WriteLine $"""{if Console.CursorLeft + p1.Remarks.Length + 10 > Console.WindowWidth then "\n " else ""}Remarks: {p1.Remarks}"""
// The example displays the following output:
// John: M, born 5/10/1992 Height 73.5 inches, Weight 207 lbs Remarks: Client since 1/3/2012
Public Class Person
Public Property Name As String
Public Property BirthDate As DateTime
Public Property Height As Double
Public Property Weight As Double
Public Property Gender As Char
Public Property Remarks As String
Public Function GetDescription() As Object()
Return { Name, Gender, Height, Weight, BirthDate}
End Function
End Class
Module Example
Public Sub Main()
Dim p1 As New Person() With { .Name = "John", .Gender = "M"c,
.BirthDate = New DateTime(1992, 5, 10),
.Height = 73.5, .Weight = 207 }
p1.Remarks = "Client since 1/3/2012"
Console.Write("{0}: {1}, born {4:d} Height {2} inches, Weight {3} lbs ",
p1.GetDescription())
If String.IsNullOrEmpty(p1.Remarks) Then
Console.WriteLine()
Else
Console.WriteLine("{1}Remarks: {0}", p1.Remarks,
If(Console.CursorLeft + p1.Remarks.Length + 10 > Console.WindowWidth,
vbCrLf + " ", ""))
End If
End Sub
End Module
' The example displays the following output:
' John: M, born 5/10/1992 Height 73.5 inches, Weight 207 lbs Remarks: Client since 1/3/2012
請注意,此範例會呼叫 Write(String, Object[]) 方法,而不是 WriteLine(String, Object[]) 方法,因為它嘗試在同一行上顯示 屬性的值 Person.Remarks
。 若要這樣做,它會檢查 和 WindowWidth 屬性的值CursorLeft,以判斷備註是否有足夠的空間可供調整。 如果有,則會顯示該行。 如果沒有,它會寫入一行,縮排三個空格,並顯示備註。
下列範例與第一個範例相同,不同之處在於它會提供五個專案清單做為 arg
自變數,而不是參數陣列。
using System;
public class Person
{
public String Name { get; set; }
public DateTime BirthDate { get; set; }
public Double Height { get; set; }
public Double Weight { get; set; }
public Char Gender { get; set; }
public String Remarks { get; set; }
public object[] GetDescription()
{
return new object[] { Name, Gender, Height, Weight, BirthDate};
}
}
public class Example
{
public static void Main()
{
var p1 = new Person() { Name = "John", Gender = 'M',
BirthDate = new DateTime(1992, 5, 10),
Height = 73.5, Weight = 207 };
p1.Remarks = "Client since 1/3/2012";
Console.Write("{0}: {1}, born {2:d} Height {3} inches, Weight {4} lbs ",
p1.Name, p1.Gender, p1.BirthDate, p1.Height, p1.Weight);
if (String.IsNullOrEmpty(p1.Remarks))
Console.WriteLine();
else
Console.WriteLine("{1}Remarks: {0}", p1.Remarks,
Console.CursorLeft + p1.Remarks.Length + 10 > Console.WindowWidth ?
"\n " : "");
}
}
// The example displays the following output:
// John: M, born 5/10/1992 Height 73.5 inches, Weight 207 lbs Remarks: Client since 1/3/2012
open System
type Person =
{ Name: string
BirthDate: DateOnly
Height: double
Weight: double
Gender: char
Remarks: string }
member this.GetDescription(): obj [] =
[| this.Name; this.Gender; this.Height; this.Weight; this.BirthDate |]
let p1 =
{ Name = "John"
Gender = 'M'
BirthDate = DateOnly(1992, 5, 10)
Height = 73.5
Weight = 207
Remarks = "Client since 1/3/2012" }
printf $"{p1.Name}: {p1.Gender}, born {p1.BirthDate:d} Height {p1.Height} inches, Weight {p1.Weight} lbs "
if String.IsNullOrEmpty p1.Remarks then
Console.WriteLine()
else
Console.WriteLine $"""{if Console.CursorLeft + p1.Remarks.Length + 10 > Console.WindowWidth then "\n " else ""}Remarks: {p1.Remarks}"""
// The example displays the following output:
// John: M, born 5/10/1992 Height 73.5 inches, Weight 207 lbs Remarks: Client since 1/3/2012
Public Class Person
Public Property Name As String
Public Property BirthDate As DateTime
Public Property Height As Double
Public Property Weight As Double
Public Property Gender As Char
Public Property Remarks As String
Public Function GetDescription() As Object()
Return { Name, Gender, Height, Weight, BirthDate}
End Function
End Class
Module Example
Public Sub Main()
Dim p1 As New Person() With { .Name = "John", .Gender = "M"c,
.BirthDate = New DateTime(1992, 5, 10),
.Height = 73.5, .Weight = 207 }
p1.Remarks = "Client since 1/3/2012"
Console.Write("{0}: {1}, born {2:d} Height {3} inches, Weight {4} lbs ",
p1.Name, p1.Gender, p1.BirthDate, p1.Height, p1.Weight)
If String.IsNullOrEmpty(p1.Remarks) Then
Console.WriteLine()
Else
Console.WriteLine("{1}Remarks: {0}", p1.Remarks,
If(Console.CursorLeft + p1.Remarks.Length + 10 > Console.WindowWidth,
vbCrLf + " ", ""))
End If
End Sub
End Module
' The example displays the following output:
' John: M, born 5/10/1992 Height 73.5 inches, Weight 207 lbs Remarks: Client since 1/3/2012
備註
這個方法會使用 .NET 的 複合格式功能 ,將物件的值轉換成其文字表示,並將該表示內嵌在字串中。 產生的字串會寫入輸出數據流。
參數 format
是由零或多個文字執行所組成,這些文字與零個或多個索引佔位元混合,稱為格式項目,對應至此方法參數清單中的物件。 格式設定程式會將每個格式專案取代為對應物件值的文字表示。
格式項目的語法是 {
index[,
alignment][:
formatString]}
,它會指定強制索引、格式化文字的選擇性長度和對齊方式,以及控制對應物件值格式化方式之格式規範字元的選擇性字符串。
.NET 提供廣泛的格式支援,如下列格式主題中更詳細地說明。
如需、 AppendFormat等方法Format所支援之複合格式功能的詳細資訊,以及的WriteLine一些多載,請參閱複合格式設定。
如需日期和時間格式規範的詳細資訊,請參閱 標準日期和時間格式字元串 和 自定義日期和時間格式字串。
如需列舉格式規範的詳細資訊,請參閱 列舉格式字串。
如需格式化的詳細資訊,請參閱 格式化類型。
參數 arg
是參數陣列。 自變數可以當做陣列或五個以上的專案清單傳遞至方法。 這些範例說明這兩種形式的方法呼叫。
給呼叫者的注意事項
C++ 程式代碼不會呼叫這個方法。 C++ 編譯程式會解析 對 System.Console.Write 的呼叫,其中包含字串和四個以上的物件參數清單,做為的 Write(String, Object, Object, Object, Object)呼叫。 它會解析 對 System.Console.Write 的呼叫,其中包含字串和物件陣列做為的 Write(String, Object)呼叫。
另請參閱
適用於
Write(String, Object)
- 來源:
- Console.cs
- 來源:
- Console.cs
- 來源:
- Console.cs
使用指定的格式資訊,將指定之物件的文字表示寫入標準輸出資料流。
public:
static void Write(System::String ^ format, System::Object ^ arg0);
public static void Write (string format, object? arg0);
public static void Write (string format, object arg0);
static member Write : string * obj -> unit
Public Shared Sub Write (format As String, arg0 As Object)
參數
- format
- String
複合格式字串。
- arg0
- Object
要使用 format
寫入的物件。
例外狀況
發生 I/O 錯誤。
format
為 null
。
format
中的格式規格無效。
範例
下列範例會使用 WriteLine
方法,示範數字、日期和列舉型別的標準格式化規範。
// This code example demonstrates the Console.WriteLine() method.
// Formatting for this example uses the "en-US" culture.
using namespace System;
public enum class Color {Yellow = 1, Blue, Green};
int main()
{
DateTime thisDate = DateTime::Now;
Console::Clear();
// Format a negative integer or floating-point number in various ways.
Console::WriteLine("Standard Numeric Format Specifiers");
Console::WriteLine(
"(C) Currency: . . . . . . . . {0:C}\n" +
"(D) Decimal:. . . . . . . . . {0:D}\n" +
"(E) Scientific: . . . . . . . {1:E}\n" +
"(F) Fixed point:. . . . . . . {1:F}\n" +
"(G) General:. . . . . . . . . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(N) Number: . . . . . . . . . {0:N}\n" +
"(P) Percent:. . . . . . . . . {1:P}\n" +
"(R) Round-trip: . . . . . . . {1:R}\n" +
"(X) Hexadecimal:. . . . . . . {0:X}\n",
-123, -123.45f);
// Format the current date in various ways.
Console::WriteLine("Standard DateTime Format Specifiers");
Console::WriteLine(
"(d) Short date: . . . . . . . {0:d}\n" +
"(D) Long date:. . . . . . . . {0:D}\n" +
"(t) Short time: . . . . . . . {0:t}\n" +
"(T) Long time:. . . . . . . . {0:T}\n" +
"(f) Full date/short time: . . {0:f}\n" +
"(F) Full date/long time:. . . {0:F}\n" +
"(g) General date/short time:. {0:g}\n" +
"(G) General date/long time: . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(M) Month:. . . . . . . . . . {0:M}\n" +
"(R) RFC1123:. . . . . . . . . {0:R}\n" +
"(s) Sortable: . . . . . . . . {0:s}\n" +
"(u) Universal sortable: . . . {0:u} (invariant)\n" +
"(U) Universal full date/time: {0:U}\n" +
"(Y) Year: . . . . . . . . . . {0:Y}\n",
thisDate);
// Format a Color enumeration value in various ways.
Console::WriteLine("Standard Enumeration Format Specifiers");
Console::WriteLine(
"(G) General:. . . . . . . . . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\n" +
"(D) Decimal number: . . . . . {0:D}\n" +
"(X) Hexadecimal:. . . . . . . {0:X}\n",
Color::Green);
};
/*
This code example produces the following results:
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: . . . . . . . 6/26/2004
(D) Long date:. . . . . . . . Saturday, June 26, 2004
(t) Short time: . . . . . . . 8:11 PM
(T) Long time:. . . . . . . . 8:11:04 PM
(f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM
(F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM
(g) General date/short time:. 6/26/2004 8:11 PM
(G) General date/long time: . 6/26/2004 8:11:04 PM
(default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G')
(M) Month:. . . . . . . . . . June 26
(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT
(s) Sortable: . . . . . . . . 2004-06-26T20:11:04
(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)
(U) Universal full date/time: Sunday, June 27, 2004 3:11:04 AM
(Y) Year: . . . . . . . . . . June, 2004
Standard Enumeration Format Specifiers
(G) General:. . . . . . . . . Green
(default):. . . . . . . . Green (default = 'G')
(F) Flags:. . . . . . . . . . Green (flags or integer)
(D) Decimal number: . . . . . 3
(X) Hexadecimal:. . . . . . . 00000003
*/
// This code example demonstrates the Console.WriteLine() method.
// Formatting for this example uses the "en-US" culture.
using System;
class Sample
{
enum Color {Yellow = 1, Blue, Green};
static DateTime thisDate = DateTime.Now;
public static void Main()
{
Console.Clear();
// Format a negative integer or floating-point number in various ways.
Console.WriteLine("Standard Numeric Format Specifiers");
Console.WriteLine(
"(C) Currency: . . . . . . . . {0:C}\n" +
"(D) Decimal:. . . . . . . . . {0:D}\n" +
"(E) Scientific: . . . . . . . {1:E}\n" +
"(F) Fixed point:. . . . . . . {1:F}\n" +
"(G) General:. . . . . . . . . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(N) Number: . . . . . . . . . {0:N}\n" +
"(P) Percent:. . . . . . . . . {1:P}\n" +
"(R) Round-trip: . . . . . . . {1:R}\n" +
"(X) Hexadecimal:. . . . . . . {0:X}\n",
-123, -123.45f);
// Format the current date in various ways.
Console.WriteLine("Standard DateTime Format Specifiers");
Console.WriteLine(
"(d) Short date: . . . . . . . {0:d}\n" +
"(D) Long date:. . . . . . . . {0:D}\n" +
"(t) Short time: . . . . . . . {0:t}\n" +
"(T) Long time:. . . . . . . . {0:T}\n" +
"(f) Full date/short time: . . {0:f}\n" +
"(F) Full date/long time:. . . {0:F}\n" +
"(g) General date/short time:. {0:g}\n" +
"(G) General date/long time: . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(M) Month:. . . . . . . . . . {0:M}\n" +
"(R) RFC1123:. . . . . . . . . {0:R}\n" +
"(s) Sortable: . . . . . . . . {0:s}\n" +
"(u) Universal sortable: . . . {0:u} (invariant)\n" +
"(U) Universal full date/time: {0:U}\n" +
"(Y) Year: . . . . . . . . . . {0:Y}\n",
thisDate);
// Format a Color enumeration value in various ways.
Console.WriteLine("Standard Enumeration Format Specifiers");
Console.WriteLine(
"(G) General:. . . . . . . . . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\n" +
"(D) Decimal number: . . . . . {0:D}\n" +
"(X) Hexadecimal:. . . . . . . {0:X}\n",
Color.Green);
}
}
/*
This code example produces the following results:
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: . . . . . . . 6/26/2004
(D) Long date:. . . . . . . . Saturday, June 26, 2004
(t) Short time: . . . . . . . 8:11 PM
(T) Long time:. . . . . . . . 8:11:04 PM
(f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM
(F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM
(g) General date/short time:. 6/26/2004 8:11 PM
(G) General date/long time: . 6/26/2004 8:11:04 PM
(default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G')
(M) Month:. . . . . . . . . . June 26
(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT
(s) Sortable: . . . . . . . . 2004-06-26T20:11:04
(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)
(U) Universal full date/time: Sunday, June 27, 2004 3:11:04 AM
(Y) Year: . . . . . . . . . . June, 2004
Standard Enumeration Format Specifiers
(G) General:. . . . . . . . . Green
(default):. . . . . . . . Green (default = 'G')
(F) Flags:. . . . . . . . . . Green (flags or integer)
(D) Decimal number: . . . . . 3
(X) Hexadecimal:. . . . . . . 00000003
*/
// This code example demonstrates the Console.WriteLine() method.
// Formatting for this example uses the "en-US" culture.
open System
type Color =
| Yellow = 1
| Blue = 2
| Green = 3
let thisDate = DateTime.Now
Console.Clear()
// Format a negative integer or floating-point number in various ways.
Console.WriteLine "Standard Numeric Format Specifiers"
Console.WriteLine(
"(C) Currency: . . . . . . . . {0:C}\n" +
"(D) Decimal:. . . . . . . . . {0:D}\n" +
"(E) Scientific: . . . . . . . {1:E}\n" +
"(F) Fixed point:. . . . . . . {1:F}\n" +
"(G) General:. . . . . . . . . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(N) Number: . . . . . . . . . {0:N}\n" +
"(P) Percent:. . . . . . . . . {1:P}\n" +
"(R) Round-trip: . . . . . . . {1:R}\n" +
"(X) Hexadecimal:. . . . . . . {0:X}\n",
-123, -123.45f)
// Format the current date in various ways.
Console.WriteLine "Standard DateTime Format Specifiers"
Console.WriteLine(
"(d) Short date: . . . . . . . {0:d}\n" +
"(D) Long date:. . . . . . . . {0:D}\n" +
"(t) Short time: . . . . . . . {0:t}\n" +
"(T) Long time:. . . . . . . . {0:T}\n" +
"(f) Full date/short time: . . {0:f}\n" +
"(F) Full date/long time:. . . {0:F}\n" +
"(g) General date/short time:. {0:g}\n" +
"(G) General date/long time: . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(M) Month:. . . . . . . . . . {0:M}\n" +
"(R) RFC1123:. . . . . . . . . {0:R}\n" +
"(s) Sortable: . . . . . . . . {0:s}\n" +
"(u) Universal sortable: . . . {0:u} (invariant)\n" +
"(U) Universal full date/time: {0:U}\n" +
"(Y) Year: . . . . . . . . . . {0:Y}\n",
thisDate)
// Format a Color enumeration value in various ways.
Console.WriteLine "Standard Enumeration Format Specifiers"
Console.WriteLine(
"(G) General:. . . . . . . . . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\n" +
"(D) Decimal number: . . . . . {0:D}\n" +
"(X) Hexadecimal:. . . . . . . {0:X}\n",
Color.Green)
// This code example produces the following results:
//
// 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: . . . . . . . 6/26/2004
// (D) Long date:. . . . . . . . Saturday, June 26, 2004
// (t) Short time: . . . . . . . 8:11 PM
// (T) Long time:. . . . . . . . 8:11:04 PM
// (f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM
// (F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM
// (g) General date/short time:. 6/26/2004 8:11 PM
// (G) General date/long time: . 6/26/2004 8:11:04 PM
// (default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G')
// (M) Month:. . . . . . . . . . June 26
// (R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT
// (s) Sortable: . . . . . . . . 2004-06-26T20:11:04
// (u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)
// (U) Universal full date/time: Sunday, June 27, 2004 3:11:04 AM
// (Y) Year: . . . . . . . . . . June, 2004
//
// Standard Enumeration Format Specifiers
// (G) General:. . . . . . . . . Green
// (default):. . . . . . . . Green (default = 'G')
// (F) Flags:. . . . . . . . . . Green (flags or integer)
// (D) Decimal number: . . . . . 3
// (X) Hexadecimal:. . . . . . . 00000003
' This code example demonstrates the Console.WriteLine() method.
' Formatting for this example uses the "en-US" culture.
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()
Console.Clear()
' Format a negative integer or floating-point number in various ways.
Console.WriteLine("Standard Numeric Format Specifiers")
Console.WriteLine("(C) Currency: . . . . . . . . {0:C}" & vbCrLf & _
"(D) Decimal:. . . . . . . . . {0:D}" & vbCrLf & _
"(E) Scientific: . . . . . . . {1:E}" & vbCrLf & _
"(F) Fixed point:. . . . . . . {1:F}" & vbCrLf & _
"(G) General:. . . . . . . . . {0:G}" & vbCrLf & _
" (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _
"(N) Number: . . . . . . . . . {0:N}" & vbCrLf & _
"(P) Percent:. . . . . . . . . {1:P}" & vbCrLf & _
"(R) Round-trip: . . . . . . . {1:R}" & vbCrLf & _
"(X) Hexadecimal:. . . . . . . {0:X}" & vbCrLf, _
- 123, - 123.45F)
' Format the current date in various ways.
Console.WriteLine("Standard DateTime Format Specifiers")
Console.WriteLine("(d) Short date: . . . . . . . {0:d}" & vbCrLf & _
"(D) Long date:. . . . . . . . {0:D}" & vbCrLf & _
"(t) Short time: . . . . . . . {0:t}" & vbCrLf & _
"(T) Long time:. . . . . . . . {0:T}" & vbCrLf & _
"(f) Full date/short time: . . {0:f}" & vbCrLf & _
"(F) Full date/long time:. . . {0:F}" & vbCrLf & _
"(g) General date/short time:. {0:g}" & vbCrLf & _
"(G) General date/long time: . {0:G}" & vbCrLf & _
" (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _
"(M) Month:. . . . . . . . . . {0:M}" & vbCrLf & _
"(R) RFC1123:. . . . . . . . . {0:R}" & vbCrLf & _
"(s) Sortable: . . . . . . . . {0:s}" & vbCrLf & _
"(u) Universal sortable: . . . {0:u} (invariant)" & vbCrLf & _
"(U) Universal full date/time: {0:U}" & vbCrLf & _
"(Y) Year: . . . . . . . . . . {0:Y}" & vbCrLf, _
thisDate)
' Format a Color enumeration value in various ways.
Console.WriteLine("Standard Enumeration Format Specifiers")
Console.WriteLine("(G) General:. . . . . . . . . {0:G}" & vbCrLf & _
" (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _
"(F) Flags:. . . . . . . . . . {0:F} (flags or integer)" & vbCrLf & _
"(D) Decimal number: . . . . . {0:D}" & vbCrLf & _
"(X) Hexadecimal:. . . . . . . {0:X}" & vbCrLf, _
Color.Green)
End Sub
End Class
'
'This code example produces the following results:
'
'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: . . . . . . . 6/26/2004
'(D) Long date:. . . . . . . . Saturday, June 26, 2004
'(t) Short time: . . . . . . . 8:11 PM
'(T) Long time:. . . . . . . . 8:11:04 PM
'(f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM
'(F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM
'(g) General date/short time:. 6/26/2004 8:11 PM
'(G) General date/long time: . 6/26/2004 8:11:04 PM
' (default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G')
'(M) Month:. . . . . . . . . . June 26
'(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT
'(s) Sortable: . . . . . . . . 2004-06-26T20:11:04
'(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)
'(U) Universal full date/time: Sunday, June 27, 2004 3:11:04 AM
'(Y) Year: . . . . . . . . . . June, 2004
'
'Standard Enumeration Format Specifiers
'(G) General:. . . . . . . . . Green
' (default):. . . . . . . . Green (default = 'G')
'(F) Flags:. . . . . . . . . . Green (flags or integer)
'(D) Decimal number: . . . . . 3
'(X) Hexadecimal:. . . . . . . 00000003
'
在下列程式碼中,說明了如何使用 Write 方法。
int main()
{
array<String^>^lineInputArr = {"1 2.2 hello TRUE","2 5.22 bye FALSE","3 6.38 see ya' TRUE"};
for ( Int32 i = 0; i < 3; i++ )
{
String^ lineInput = lineInputArr->GetValue( i )->ToString();
String^ aChar = "\t";
array<String^>^fields = lineInput->Split( aChar->ToCharArray() );
Boolean isFirstField = true;
for ( Int32 i = 0; i < fields->Length; i++ )
{
if ( isFirstField )
isFirstField = false;
else
Console::Write( "," );
// If the field represents a boolean, replace with a numeric representation.
try
{
Console::Write( Convert::ToByte( Convert::ToBoolean( fields[ i ] ) ) );
}
catch ( FormatException^ )
{
Console::Write( fields[ i ] );
}
}
Console::WriteLine();
}
}
public class FormatConverter
{
public static void Main(string[] args)
{
string lineInput;
while ((lineInput = Console.ReadLine()) != null)
{
string[] fields = lineInput.Split(new char[] {'\t'});
bool isFirstField = true;
foreach (var item in fields)
{
if (isFirstField)
isFirstField = false;
else
Console.Write(',');
// If the field represents a boolean, replace with a numeric representation.
bool itemBool;
if (Boolean.TryParse(item, out itemBool))
Console.Write(Convert.ToByte(itemBool));
else
Console.Write(item);
}
Console.WriteLine();
}
}
}
let mutable lineInput = Console.ReadLine()
while lineInput <> null do
let fields = lineInput.Split '\t'
let mutable isFirstField = true
for item in fields do
if isFirstField then
isFirstField <- false
else
Console.Write ','
// If the field represents a boolean, replace with a numeric representation.
match Boolean.TryParse item with
| true, itemBool ->
Console.Write(Convert.ToByte itemBool)
| _ ->
Console.Write item
Console.WriteLine()
Public Class FormatConverter
Public Shared Sub Main()
Dim lineInput As String = Console.ReadLine()
While Not lineInput Is Nothing
Dim fields As String() = lineInput.Split(ControlChars.Tab)
Dim isFirstField As Boolean = True
For Each item As String In fields
If isFirstField Then
isFirstField = False
Else
Console.Write(",")
End If
' If the field represents a boolean, replace with a numeric representation.
Dim itemBool As Boolean
If Boolean.TryParse(item, itemBool)
Console.Write(Convert.ToByte(itemBool))
Else
Console.Write(item)
End If
Next
Console.WriteLine()
lineInput = Console.ReadLine()
End While
End Sub
End Class
備註
這個方法會使用 .NET 的 複合格式功能 ,將物件的值轉換成其文字表示,並將該表示內嵌在字串中。 產生的字串會寫入輸出數據流。
參數 format
是由零或多個文字執行所組成,這些文字與零個或多個索引佔位元混合,稱為格式項目,對應至此方法參數清單中的物件。 格式設定程式會將每個格式專案取代為對應物件值的文字表示。
格式項目的語法是 {
index[,
alignment][:
formatString]}
,它會指定強制索引、格式化文字的選擇性長度和對齊方式,以及控制對應物件值格式化方式之格式規範字元的選擇性字符串。
.NET 提供廣泛的格式支援,如下列格式主題中更詳細地說明。
如需、 AppendFormat等方法Format所支援之複合格式功能的詳細資訊,以及的WriteLine一些多載,請參閱複合格式設定。
如需日期和時間格式規範的詳細資訊,請參閱 標準日期和時間格式字元串 和 自定義日期和時間格式字串。
如需列舉格式規範的詳細資訊,請參閱 列舉格式字串。
如需格式化的詳細資訊,請參閱 格式化類型。
另請參閱
適用於
Write(UInt64)
- 來源:
- Console.cs
- 來源:
- Console.cs
- 來源:
- Console.cs
重要
此 API 不符合 CLS 規範。
將指定 64 位元不帶正負號整數值的文字表示寫入標準輸出資料流。
public:
static void Write(System::UInt64 value);
[System.CLSCompliant(false)]
public static void Write (ulong value);
[<System.CLSCompliant(false)>]
static member Write : uint64 -> unit
Public Shared Sub Write (value As ULong)
參數
- value
- UInt64
要寫入的值。
- 屬性
例外狀況
發生 I/O 錯誤。
範例
在下列程式碼中,說明了如何使用 Write
方法。
int main()
{
array<String^>^lineInputArr = {"1 2.2 hello TRUE","2 5.22 bye FALSE","3 6.38 see ya' TRUE"};
for ( Int32 i = 0; i < 3; i++ )
{
String^ lineInput = lineInputArr->GetValue( i )->ToString();
String^ aChar = "\t";
array<String^>^fields = lineInput->Split( aChar->ToCharArray() );
Boolean isFirstField = true;
for ( Int32 i = 0; i < fields->Length; i++ )
{
if ( isFirstField )
isFirstField = false;
else
Console::Write( "," );
// If the field represents a boolean, replace with a numeric representation.
try
{
Console::Write( Convert::ToByte( Convert::ToBoolean( fields[ i ] ) ) );
}
catch ( FormatException^ )
{
Console::Write( fields[ i ] );
}
}
Console::WriteLine();
}
}
public class FormatConverter
{
public static void Main(string[] args)
{
string lineInput;
while ((lineInput = Console.ReadLine()) != null)
{
string[] fields = lineInput.Split(new char[] {'\t'});
bool isFirstField = true;
foreach (var item in fields)
{
if (isFirstField)
isFirstField = false;
else
Console.Write(',');
// If the field represents a boolean, replace with a numeric representation.
bool itemBool;
if (Boolean.TryParse(item, out itemBool))
Console.Write(Convert.ToByte(itemBool));
else
Console.Write(item);
}
Console.WriteLine();
}
}
}
let mutable lineInput = Console.ReadLine()
while lineInput <> null do
let fields = lineInput.Split '\t'
let mutable isFirstField = true
for item in fields do
if isFirstField then
isFirstField <- false
else
Console.Write ','
// If the field represents a boolean, replace with a numeric representation.
match Boolean.TryParse item with
| true, itemBool ->
Console.Write(Convert.ToByte itemBool)
| _ ->
Console.Write item
Console.WriteLine()
Public Class FormatConverter
Public Shared Sub Main()
Dim lineInput As String = Console.ReadLine()
While Not lineInput Is Nothing
Dim fields As String() = lineInput.Split(ControlChars.Tab)
Dim isFirstField As Boolean = True
For Each item As String In fields
If isFirstField Then
isFirstField = False
Else
Console.Write(",")
End If
' If the field represents a boolean, replace with a numeric representation.
Dim itemBool As Boolean
If Boolean.TryParse(item, itemBool)
Console.Write(Convert.ToByte(itemBool))
Else
Console.Write(item)
End If
Next
Console.WriteLine()
lineInput = Console.ReadLine()
End While
End Sub
End Class
備註
的文字表示 value
是藉由呼叫 UInt64.ToString 方法來產生。
另請參閱
適用於
Write(UInt32)
- 來源:
- Console.cs
- 來源:
- Console.cs
- 來源:
- Console.cs
重要
此 API 不符合 CLS 規範。
將指定 32 位元不帶正負號整數值的文字表示寫入標準輸出資料流。
public:
static void Write(System::UInt32 value);
[System.CLSCompliant(false)]
public static void Write (uint value);
[<System.CLSCompliant(false)>]
static member Write : uint32 -> unit
Public Shared Sub Write (value As UInteger)
參數
- value
- UInt32
要寫入的值。
- 屬性
例外狀況
發生 I/O 錯誤。
範例
在下列程式碼中,說明了如何使用 Write
方法。
int main()
{
array<String^>^lineInputArr = {"1 2.2 hello TRUE","2 5.22 bye FALSE","3 6.38 see ya' TRUE"};
for ( Int32 i = 0; i < 3; i++ )
{
String^ lineInput = lineInputArr->GetValue( i )->ToString();
String^ aChar = "\t";
array<String^>^fields = lineInput->Split( aChar->ToCharArray() );
Boolean isFirstField = true;
for ( Int32 i = 0; i < fields->Length; i++ )
{
if ( isFirstField )
isFirstField = false;
else
Console::Write( "," );
// If the field represents a boolean, replace with a numeric representation.
try
{
Console::Write( Convert::ToByte( Convert::ToBoolean( fields[ i ] ) ) );
}
catch ( FormatException^ )
{
Console::Write( fields[ i ] );
}
}
Console::WriteLine();
}
}
public class FormatConverter
{
public static void Main(string[] args)
{
string lineInput;
while ((lineInput = Console.ReadLine()) != null)
{
string[] fields = lineInput.Split(new char[] {'\t'});
bool isFirstField = true;
foreach (var item in fields)
{
if (isFirstField)
isFirstField = false;
else
Console.Write(',');
// If the field represents a boolean, replace with a numeric representation.
bool itemBool;
if (Boolean.TryParse(item, out itemBool))
Console.Write(Convert.ToByte(itemBool));
else
Console.Write(item);
}
Console.WriteLine();
}
}
}
let mutable lineInput = Console.ReadLine()
while lineInput <> null do
let fields = lineInput.Split '\t'
let mutable isFirstField = true
for item in fields do
if isFirstField then
isFirstField <- false
else
Console.Write ','
// If the field represents a boolean, replace with a numeric representation.
match Boolean.TryParse item with
| true, itemBool ->
Console.Write(Convert.ToByte itemBool)
| _ ->
Console.Write item
Console.WriteLine()
Public Class FormatConverter
Public Shared Sub Main()
Dim lineInput As String = Console.ReadLine()
While Not lineInput Is Nothing
Dim fields As String() = lineInput.Split(ControlChars.Tab)
Dim isFirstField As Boolean = True
For Each item As String In fields
If isFirstField Then
isFirstField = False
Else
Console.Write(",")
End If
' If the field represents a boolean, replace with a numeric representation.
Dim itemBool As Boolean
If Boolean.TryParse(item, itemBool)
Console.Write(Convert.ToByte(itemBool))
Else
Console.Write(item)
End If
Next
Console.WriteLine()
lineInput = Console.ReadLine()
End While
End Sub
End Class
備註
的文字表示 value
是藉由呼叫 UInt32.ToString 方法來產生。
另請參閱
適用於
Write(String)
- 來源:
- Console.cs
- 來源:
- Console.cs
- 來源:
- Console.cs
將指定的字串值寫入標準輸出資料流。
public:
static void Write(System::String ^ value);
public static void Write (string? value);
public static void Write (string value);
static member Write : string -> unit
Public Shared Sub Write (value As String)
參數
- value
- String
要寫入的值。
例外狀況
發生 I/O 錯誤。
範例
在下列程式碼中,說明了如何使用 Write
方法。
int main()
{
array<String^>^lineInputArr = {"1 2.2 hello TRUE","2 5.22 bye FALSE","3 6.38 see ya' TRUE"};
for ( Int32 i = 0; i < 3; i++ )
{
String^ lineInput = lineInputArr->GetValue( i )->ToString();
String^ aChar = "\t";
array<String^>^fields = lineInput->Split( aChar->ToCharArray() );
Boolean isFirstField = true;
for ( Int32 i = 0; i < fields->Length; i++ )
{
if ( isFirstField )
isFirstField = false;
else
Console::Write( "," );
// If the field represents a boolean, replace with a numeric representation.
try
{
Console::Write( Convert::ToByte( Convert::ToBoolean( fields[ i ] ) ) );
}
catch ( FormatException^ )
{
Console::Write( fields[ i ] );
}
}
Console::WriteLine();
}
}
public class FormatConverter
{
public static void Main(string[] args)
{
string lineInput;
while ((lineInput = Console.ReadLine()) != null)
{
string[] fields = lineInput.Split(new char[] {'\t'});
bool isFirstField = true;
foreach (var item in fields)
{
if (isFirstField)
isFirstField = false;
else
Console.Write(',');
// If the field represents a boolean, replace with a numeric representation.
bool itemBool;
if (Boolean.TryParse(item, out itemBool))
Console.Write(Convert.ToByte(itemBool));
else
Console.Write(item);
}
Console.WriteLine();
}
}
}
let mutable lineInput = Console.ReadLine()
while lineInput <> null do
let fields = lineInput.Split '\t'
let mutable isFirstField = true
for item in fields do
if isFirstField then
isFirstField <- false
else
Console.Write ','
// If the field represents a boolean, replace with a numeric representation.
match Boolean.TryParse item with
| true, itemBool ->
Console.Write(Convert.ToByte itemBool)
| _ ->
Console.Write item
Console.WriteLine()
Public Class FormatConverter
Public Shared Sub Main()
Dim lineInput As String = Console.ReadLine()
While Not lineInput Is Nothing
Dim fields As String() = lineInput.Split(ControlChars.Tab)
Dim isFirstField As Boolean = True
For Each item As String In fields
If isFirstField Then
isFirstField = False
Else
Console.Write(",")
End If
' If the field represents a boolean, replace with a numeric representation.
Dim itemBool As Boolean
If Boolean.TryParse(item, itemBool)
Console.Write(Convert.ToByte(itemBool))
Else
Console.Write(item)
End If
Next
Console.WriteLine()
lineInput = Console.ReadLine()
End While
End Sub
End Class
備註
如果 value 為 null
,則不會將任何內容寫入標準輸出數據流。
另請參閱
適用於
Write(String, Object, Object)
- 來源:
- Console.cs
- 來源:
- Console.cs
- 來源:
- Console.cs
使用指定的格式資訊,將指定之物件的文字表示寫入標準輸出資料流。
public:
static void Write(System::String ^ format, System::Object ^ arg0, System::Object ^ arg1);
public static void Write (string format, object? arg0, object? arg1);
public static void Write (string format, object arg0, object arg1);
static member Write : string * obj * obj -> unit
Public Shared Sub Write (format As String, arg0 As Object, arg1 As Object)
參數
- format
- String
複合格式字串。
- arg0
- Object
第一個物件,使用 format
寫入。
- arg1
- Object
第二個物件,使用 format
寫入。
例外狀況
發生 I/O 錯誤。
format
為 null
。
format
中的格式規格無效。
範例
下列範例會使用 WriteLine
方法,示範數字、日期和列舉型別的標準格式化規範。
// This code example demonstrates the Console.WriteLine() method.
// Formatting for this example uses the "en-US" culture.
using namespace System;
public enum class Color {Yellow = 1, Blue, Green};
int main()
{
DateTime thisDate = DateTime::Now;
Console::Clear();
// Format a negative integer or floating-point number in various ways.
Console::WriteLine("Standard Numeric Format Specifiers");
Console::WriteLine(
"(C) Currency: . . . . . . . . {0:C}\n" +
"(D) Decimal:. . . . . . . . . {0:D}\n" +
"(E) Scientific: . . . . . . . {1:E}\n" +
"(F) Fixed point:. . . . . . . {1:F}\n" +
"(G) General:. . . . . . . . . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(N) Number: . . . . . . . . . {0:N}\n" +
"(P) Percent:. . . . . . . . . {1:P}\n" +
"(R) Round-trip: . . . . . . . {1:R}\n" +
"(X) Hexadecimal:. . . . . . . {0:X}\n",
-123, -123.45f);
// Format the current date in various ways.
Console::WriteLine("Standard DateTime Format Specifiers");
Console::WriteLine(
"(d) Short date: . . . . . . . {0:d}\n" +
"(D) Long date:. . . . . . . . {0:D}\n" +
"(t) Short time: . . . . . . . {0:t}\n" +
"(T) Long time:. . . . . . . . {0:T}\n" +
"(f) Full date/short time: . . {0:f}\n" +
"(F) Full date/long time:. . . {0:F}\n" +
"(g) General date/short time:. {0:g}\n" +
"(G) General date/long time: . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(M) Month:. . . . . . . . . . {0:M}\n" +
"(R) RFC1123:. . . . . . . . . {0:R}\n" +
"(s) Sortable: . . . . . . . . {0:s}\n" +
"(u) Universal sortable: . . . {0:u} (invariant)\n" +
"(U) Universal full date/time: {0:U}\n" +
"(Y) Year: . . . . . . . . . . {0:Y}\n",
thisDate);
// Format a Color enumeration value in various ways.
Console::WriteLine("Standard Enumeration Format Specifiers");
Console::WriteLine(
"(G) General:. . . . . . . . . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\n" +
"(D) Decimal number: . . . . . {0:D}\n" +
"(X) Hexadecimal:. . . . . . . {0:X}\n",
Color::Green);
};
/*
This code example produces the following results:
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: . . . . . . . 6/26/2004
(D) Long date:. . . . . . . . Saturday, June 26, 2004
(t) Short time: . . . . . . . 8:11 PM
(T) Long time:. . . . . . . . 8:11:04 PM
(f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM
(F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM
(g) General date/short time:. 6/26/2004 8:11 PM
(G) General date/long time: . 6/26/2004 8:11:04 PM
(default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G')
(M) Month:. . . . . . . . . . June 26
(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT
(s) Sortable: . . . . . . . . 2004-06-26T20:11:04
(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)
(U) Universal full date/time: Sunday, June 27, 2004 3:11:04 AM
(Y) Year: . . . . . . . . . . June, 2004
Standard Enumeration Format Specifiers
(G) General:. . . . . . . . . Green
(default):. . . . . . . . Green (default = 'G')
(F) Flags:. . . . . . . . . . Green (flags or integer)
(D) Decimal number: . . . . . 3
(X) Hexadecimal:. . . . . . . 00000003
*/
// This code example demonstrates the Console.WriteLine() method.
// Formatting for this example uses the "en-US" culture.
using System;
class Sample
{
enum Color {Yellow = 1, Blue, Green};
static DateTime thisDate = DateTime.Now;
public static void Main()
{
Console.Clear();
// Format a negative integer or floating-point number in various ways.
Console.WriteLine("Standard Numeric Format Specifiers");
Console.WriteLine(
"(C) Currency: . . . . . . . . {0:C}\n" +
"(D) Decimal:. . . . . . . . . {0:D}\n" +
"(E) Scientific: . . . . . . . {1:E}\n" +
"(F) Fixed point:. . . . . . . {1:F}\n" +
"(G) General:. . . . . . . . . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(N) Number: . . . . . . . . . {0:N}\n" +
"(P) Percent:. . . . . . . . . {1:P}\n" +
"(R) Round-trip: . . . . . . . {1:R}\n" +
"(X) Hexadecimal:. . . . . . . {0:X}\n",
-123, -123.45f);
// Format the current date in various ways.
Console.WriteLine("Standard DateTime Format Specifiers");
Console.WriteLine(
"(d) Short date: . . . . . . . {0:d}\n" +
"(D) Long date:. . . . . . . . {0:D}\n" +
"(t) Short time: . . . . . . . {0:t}\n" +
"(T) Long time:. . . . . . . . {0:T}\n" +
"(f) Full date/short time: . . {0:f}\n" +
"(F) Full date/long time:. . . {0:F}\n" +
"(g) General date/short time:. {0:g}\n" +
"(G) General date/long time: . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(M) Month:. . . . . . . . . . {0:M}\n" +
"(R) RFC1123:. . . . . . . . . {0:R}\n" +
"(s) Sortable: . . . . . . . . {0:s}\n" +
"(u) Universal sortable: . . . {0:u} (invariant)\n" +
"(U) Universal full date/time: {0:U}\n" +
"(Y) Year: . . . . . . . . . . {0:Y}\n",
thisDate);
// Format a Color enumeration value in various ways.
Console.WriteLine("Standard Enumeration Format Specifiers");
Console.WriteLine(
"(G) General:. . . . . . . . . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\n" +
"(D) Decimal number: . . . . . {0:D}\n" +
"(X) Hexadecimal:. . . . . . . {0:X}\n",
Color.Green);
}
}
/*
This code example produces the following results:
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: . . . . . . . 6/26/2004
(D) Long date:. . . . . . . . Saturday, June 26, 2004
(t) Short time: . . . . . . . 8:11 PM
(T) Long time:. . . . . . . . 8:11:04 PM
(f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM
(F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM
(g) General date/short time:. 6/26/2004 8:11 PM
(G) General date/long time: . 6/26/2004 8:11:04 PM
(default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G')
(M) Month:. . . . . . . . . . June 26
(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT
(s) Sortable: . . . . . . . . 2004-06-26T20:11:04
(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)
(U) Universal full date/time: Sunday, June 27, 2004 3:11:04 AM
(Y) Year: . . . . . . . . . . June, 2004
Standard Enumeration Format Specifiers
(G) General:. . . . . . . . . Green
(default):. . . . . . . . Green (default = 'G')
(F) Flags:. . . . . . . . . . Green (flags or integer)
(D) Decimal number: . . . . . 3
(X) Hexadecimal:. . . . . . . 00000003
*/
// This code example demonstrates the Console.WriteLine() method.
// Formatting for this example uses the "en-US" culture.
open System
type Color =
| Yellow = 1
| Blue = 2
| Green = 3
let thisDate = DateTime.Now
Console.Clear()
// Format a negative integer or floating-point number in various ways.
Console.WriteLine "Standard Numeric Format Specifiers"
Console.WriteLine(
"(C) Currency: . . . . . . . . {0:C}\n" +
"(D) Decimal:. . . . . . . . . {0:D}\n" +
"(E) Scientific: . . . . . . . {1:E}\n" +
"(F) Fixed point:. . . . . . . {1:F}\n" +
"(G) General:. . . . . . . . . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(N) Number: . . . . . . . . . {0:N}\n" +
"(P) Percent:. . . . . . . . . {1:P}\n" +
"(R) Round-trip: . . . . . . . {1:R}\n" +
"(X) Hexadecimal:. . . . . . . {0:X}\n",
-123, -123.45f)
// Format the current date in various ways.
Console.WriteLine "Standard DateTime Format Specifiers"
Console.WriteLine(
"(d) Short date: . . . . . . . {0:d}\n" +
"(D) Long date:. . . . . . . . {0:D}\n" +
"(t) Short time: . . . . . . . {0:t}\n" +
"(T) Long time:. . . . . . . . {0:T}\n" +
"(f) Full date/short time: . . {0:f}\n" +
"(F) Full date/long time:. . . {0:F}\n" +
"(g) General date/short time:. {0:g}\n" +
"(G) General date/long time: . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(M) Month:. . . . . . . . . . {0:M}\n" +
"(R) RFC1123:. . . . . . . . . {0:R}\n" +
"(s) Sortable: . . . . . . . . {0:s}\n" +
"(u) Universal sortable: . . . {0:u} (invariant)\n" +
"(U) Universal full date/time: {0:U}\n" +
"(Y) Year: . . . . . . . . . . {0:Y}\n",
thisDate)
// Format a Color enumeration value in various ways.
Console.WriteLine "Standard Enumeration Format Specifiers"
Console.WriteLine(
"(G) General:. . . . . . . . . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\n" +
"(D) Decimal number: . . . . . {0:D}\n" +
"(X) Hexadecimal:. . . . . . . {0:X}\n",
Color.Green)
// This code example produces the following results:
//
// 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: . . . . . . . 6/26/2004
// (D) Long date:. . . . . . . . Saturday, June 26, 2004
// (t) Short time: . . . . . . . 8:11 PM
// (T) Long time:. . . . . . . . 8:11:04 PM
// (f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM
// (F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM
// (g) General date/short time:. 6/26/2004 8:11 PM
// (G) General date/long time: . 6/26/2004 8:11:04 PM
// (default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G')
// (M) Month:. . . . . . . . . . June 26
// (R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT
// (s) Sortable: . . . . . . . . 2004-06-26T20:11:04
// (u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)
// (U) Universal full date/time: Sunday, June 27, 2004 3:11:04 AM
// (Y) Year: . . . . . . . . . . June, 2004
//
// Standard Enumeration Format Specifiers
// (G) General:. . . . . . . . . Green
// (default):. . . . . . . . Green (default = 'G')
// (F) Flags:. . . . . . . . . . Green (flags or integer)
// (D) Decimal number: . . . . . 3
// (X) Hexadecimal:. . . . . . . 00000003
' This code example demonstrates the Console.WriteLine() method.
' Formatting for this example uses the "en-US" culture.
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()
Console.Clear()
' Format a negative integer or floating-point number in various ways.
Console.WriteLine("Standard Numeric Format Specifiers")
Console.WriteLine("(C) Currency: . . . . . . . . {0:C}" & vbCrLf & _
"(D) Decimal:. . . . . . . . . {0:D}" & vbCrLf & _
"(E) Scientific: . . . . . . . {1:E}" & vbCrLf & _
"(F) Fixed point:. . . . . . . {1:F}" & vbCrLf & _
"(G) General:. . . . . . . . . {0:G}" & vbCrLf & _
" (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _
"(N) Number: . . . . . . . . . {0:N}" & vbCrLf & _
"(P) Percent:. . . . . . . . . {1:P}" & vbCrLf & _
"(R) Round-trip: . . . . . . . {1:R}" & vbCrLf & _
"(X) Hexadecimal:. . . . . . . {0:X}" & vbCrLf, _
- 123, - 123.45F)
' Format the current date in various ways.
Console.WriteLine("Standard DateTime Format Specifiers")
Console.WriteLine("(d) Short date: . . . . . . . {0:d}" & vbCrLf & _
"(D) Long date:. . . . . . . . {0:D}" & vbCrLf & _
"(t) Short time: . . . . . . . {0:t}" & vbCrLf & _
"(T) Long time:. . . . . . . . {0:T}" & vbCrLf & _
"(f) Full date/short time: . . {0:f}" & vbCrLf & _
"(F) Full date/long time:. . . {0:F}" & vbCrLf & _
"(g) General date/short time:. {0:g}" & vbCrLf & _
"(G) General date/long time: . {0:G}" & vbCrLf & _
" (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _
"(M) Month:. . . . . . . . . . {0:M}" & vbCrLf & _
"(R) RFC1123:. . . . . . . . . {0:R}" & vbCrLf & _
"(s) Sortable: . . . . . . . . {0:s}" & vbCrLf & _
"(u) Universal sortable: . . . {0:u} (invariant)" & vbCrLf & _
"(U) Universal full date/time: {0:U}" & vbCrLf & _
"(Y) Year: . . . . . . . . . . {0:Y}" & vbCrLf, _
thisDate)
' Format a Color enumeration value in various ways.
Console.WriteLine("Standard Enumeration Format Specifiers")
Console.WriteLine("(G) General:. . . . . . . . . {0:G}" & vbCrLf & _
" (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _
"(F) Flags:. . . . . . . . . . {0:F} (flags or integer)" & vbCrLf & _
"(D) Decimal number: . . . . . {0:D}" & vbCrLf & _
"(X) Hexadecimal:. . . . . . . {0:X}" & vbCrLf, _
Color.Green)
End Sub
End Class
'
'This code example produces the following results:
'
'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: . . . . . . . 6/26/2004
'(D) Long date:. . . . . . . . Saturday, June 26, 2004
'(t) Short time: . . . . . . . 8:11 PM
'(T) Long time:. . . . . . . . 8:11:04 PM
'(f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM
'(F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM
'(g) General date/short time:. 6/26/2004 8:11 PM
'(G) General date/long time: . 6/26/2004 8:11:04 PM
' (default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G')
'(M) Month:. . . . . . . . . . June 26
'(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT
'(s) Sortable: . . . . . . . . 2004-06-26T20:11:04
'(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)
'(U) Universal full date/time: Sunday, June 27, 2004 3:11:04 AM
'(Y) Year: . . . . . . . . . . June, 2004
'
'Standard Enumeration Format Specifiers
'(G) General:. . . . . . . . . Green
' (default):. . . . . . . . Green (default = 'G')
'(F) Flags:. . . . . . . . . . Green (flags or integer)
'(D) Decimal number: . . . . . 3
'(X) Hexadecimal:. . . . . . . 00000003
'
在下列程式碼中,說明了如何使用 Write
方法。
int main()
{
array<String^>^lineInputArr = {"1 2.2 hello TRUE","2 5.22 bye FALSE","3 6.38 see ya' TRUE"};
for ( Int32 i = 0; i < 3; i++ )
{
String^ lineInput = lineInputArr->GetValue( i )->ToString();
String^ aChar = "\t";
array<String^>^fields = lineInput->Split( aChar->ToCharArray() );
Boolean isFirstField = true;
for ( Int32 i = 0; i < fields->Length; i++ )
{
if ( isFirstField )
isFirstField = false;
else
Console::Write( "," );
// If the field represents a boolean, replace with a numeric representation.
try
{
Console::Write( Convert::ToByte( Convert::ToBoolean( fields[ i ] ) ) );
}
catch ( FormatException^ )
{
Console::Write( fields[ i ] );
}
}
Console::WriteLine();
}
}
public class FormatConverter
{
public static void Main(string[] args)
{
string lineInput;
while ((lineInput = Console.ReadLine()) != null)
{
string[] fields = lineInput.Split(new char[] {'\t'});
bool isFirstField = true;
foreach (var item in fields)
{
if (isFirstField)
isFirstField = false;
else
Console.Write(',');
// If the field represents a boolean, replace with a numeric representation.
bool itemBool;
if (Boolean.TryParse(item, out itemBool))
Console.Write(Convert.ToByte(itemBool));
else
Console.Write(item);
}
Console.WriteLine();
}
}
}
let mutable lineInput = Console.ReadLine()
while lineInput <> null do
let fields = lineInput.Split '\t'
let mutable isFirstField = true
for item in fields do
if isFirstField then
isFirstField <- false
else
Console.Write ','
// If the field represents a boolean, replace with a numeric representation.
match Boolean.TryParse item with
| true, itemBool ->
Console.Write(Convert.ToByte itemBool)
| _ ->
Console.Write item
Console.WriteLine()
Public Class FormatConverter
Public Shared Sub Main()
Dim lineInput As String = Console.ReadLine()
While Not lineInput Is Nothing
Dim fields As String() = lineInput.Split(ControlChars.Tab)
Dim isFirstField As Boolean = True
For Each item As String In fields
If isFirstField Then
isFirstField = False
Else
Console.Write(",")
End If
' If the field represents a boolean, replace with a numeric representation.
Dim itemBool As Boolean
If Boolean.TryParse(item, itemBool)
Console.Write(Convert.ToByte(itemBool))
Else
Console.Write(item)
End If
Next
Console.WriteLine()
lineInput = Console.ReadLine()
End While
End Sub
End Class
備註
這個方法會使用 .NET 的 複合格式功能 ,將物件的值轉換成其文字表示,並將該表示內嵌在字串中。 產生的字串會寫入輸出數據流。
參數 format
是由零或多個文字執行所組成,這些文字與零個或多個索引佔位元混合,稱為格式項目,對應至此方法參數清單中的物件。 格式設定程式會將每個格式專案取代為對應物件值的文字表示。
格式項目的語法是 {
index[,
alignment][:
formatString]}
,它會指定強制索引、格式化文字的選擇性長度和對齊方式,以及控制對應物件值格式化方式之格式規範字元的選擇性字符串。
.NET 提供廣泛的格式支援,如下列格式主題中更詳細地說明。
如需、 AppendFormat等方法Format所支援之複合格式功能的詳細資訊,以及的WriteLine一些多載,請參閱複合格式設定。
如需日期和時間格式規範的詳細資訊,請參閱 標準日期和時間格式字元串 和 自定義日期和時間格式字串。
如需列舉格式規範的詳細資訊,請參閱 列舉格式字串。
如需格式化的詳細資訊,請參閱 格式化類型。
另請參閱
適用於
Write(Object)
- 來源:
- Console.cs
- 來源:
- Console.cs
- 來源:
- Console.cs
將指定物件的文字表示寫入標準輸出資料流。
public:
static void Write(System::Object ^ value);
public static void Write (object? value);
public static void Write (object value);
static member Write : obj -> unit
Public Shared Sub Write (value As Object)
參數
- value
- Object
要寫入的值,或 null
。
例外狀況
發生 I/O 錯誤。
範例
在下列程式碼中,說明了如何使用 Write
方法。
int main()
{
array<String^>^lineInputArr = {"1 2.2 hello TRUE","2 5.22 bye FALSE","3 6.38 see ya' TRUE"};
for ( Int32 i = 0; i < 3; i++ )
{
String^ lineInput = lineInputArr->GetValue( i )->ToString();
String^ aChar = "\t";
array<String^>^fields = lineInput->Split( aChar->ToCharArray() );
Boolean isFirstField = true;
for ( Int32 i = 0; i < fields->Length; i++ )
{
if ( isFirstField )
isFirstField = false;
else
Console::Write( "," );
// If the field represents a boolean, replace with a numeric representation.
try
{
Console::Write( Convert::ToByte( Convert::ToBoolean( fields[ i ] ) ) );
}
catch ( FormatException^ )
{
Console::Write( fields[ i ] );
}
}
Console::WriteLine();
}
}
public class FormatConverter
{
public static void Main(string[] args)
{
string lineInput;
while ((lineInput = Console.ReadLine()) != null)
{
string[] fields = lineInput.Split(new char[] {'\t'});
bool isFirstField = true;
foreach (var item in fields)
{
if (isFirstField)
isFirstField = false;
else
Console.Write(',');
// If the field represents a boolean, replace with a numeric representation.
bool itemBool;
if (Boolean.TryParse(item, out itemBool))
Console.Write(Convert.ToByte(itemBool));
else
Console.Write(item);
}
Console.WriteLine();
}
}
}
let mutable lineInput = Console.ReadLine()
while lineInput <> null do
let fields = lineInput.Split '\t'
let mutable isFirstField = true
for item in fields do
if isFirstField then
isFirstField <- false
else
Console.Write ','
// If the field represents a boolean, replace with a numeric representation.
match Boolean.TryParse item with
| true, itemBool ->
Console.Write(Convert.ToByte itemBool)
| _ ->
Console.Write item
Console.WriteLine()
Public Class FormatConverter
Public Shared Sub Main()
Dim lineInput As String = Console.ReadLine()
While Not lineInput Is Nothing
Dim fields As String() = lineInput.Split(ControlChars.Tab)
Dim isFirstField As Boolean = True
For Each item As String In fields
If isFirstField Then
isFirstField = False
Else
Console.Write(",")
End If
' If the field represents a boolean, replace with a numeric representation.
Dim itemBool As Boolean
If Boolean.TryParse(item, itemBool)
Console.Write(Convert.ToByte(itemBool))
Else
Console.Write(item)
End If
Next
Console.WriteLine()
lineInput = Console.ReadLine()
End While
End Sub
End Class
備註
如果 value
為 null
,則不會寫入任何專案,也不會擲回任何例外狀況。 否則,會 ToString
呼叫的 value
方法來產生其字串表示,並將產生的字串寫入標準輸出數據流。
另請參閱
適用於
Write(Single)
- 來源:
- Console.cs
- 來源:
- Console.cs
- 來源:
- Console.cs
將指定的單精確度浮點數值的文字表示寫入標準輸出資料流。
public:
static void Write(float value);
public static void Write (float value);
static member Write : single -> unit
Public Shared Sub Write (value As Single)
參數
- value
- Single
要寫入的值。
例外狀況
發生 I/O 錯誤。
範例
在下列程式碼中,說明了如何使用 Write
方法。
int main()
{
array<String^>^lineInputArr = {"1 2.2 hello TRUE","2 5.22 bye FALSE","3 6.38 see ya' TRUE"};
for ( Int32 i = 0; i < 3; i++ )
{
String^ lineInput = lineInputArr->GetValue( i )->ToString();
String^ aChar = "\t";
array<String^>^fields = lineInput->Split( aChar->ToCharArray() );
Boolean isFirstField = true;
for ( Int32 i = 0; i < fields->Length; i++ )
{
if ( isFirstField )
isFirstField = false;
else
Console::Write( "," );
// If the field represents a boolean, replace with a numeric representation.
try
{
Console::Write( Convert::ToByte( Convert::ToBoolean( fields[ i ] ) ) );
}
catch ( FormatException^ )
{
Console::Write( fields[ i ] );
}
}
Console::WriteLine();
}
}
public class FormatConverter
{
public static void Main(string[] args)
{
string lineInput;
while ((lineInput = Console.ReadLine()) != null)
{
string[] fields = lineInput.Split(new char[] {'\t'});
bool isFirstField = true;
foreach (var item in fields)
{
if (isFirstField)
isFirstField = false;
else
Console.Write(',');
// If the field represents a boolean, replace with a numeric representation.
bool itemBool;
if (Boolean.TryParse(item, out itemBool))
Console.Write(Convert.ToByte(itemBool));
else
Console.Write(item);
}
Console.WriteLine();
}
}
}
let mutable lineInput = Console.ReadLine()
while lineInput <> null do
let fields = lineInput.Split '\t'
let mutable isFirstField = true
for item in fields do
if isFirstField then
isFirstField <- false
else
Console.Write ','
// If the field represents a boolean, replace with a numeric representation.
match Boolean.TryParse item with
| true, itemBool ->
Console.Write(Convert.ToByte itemBool)
| _ ->
Console.Write item
Console.WriteLine()
Public Class FormatConverter
Public Shared Sub Main()
Dim lineInput As String = Console.ReadLine()
While Not lineInput Is Nothing
Dim fields As String() = lineInput.Split(ControlChars.Tab)
Dim isFirstField As Boolean = True
For Each item As String In fields
If isFirstField Then
isFirstField = False
Else
Console.Write(",")
End If
' If the field represents a boolean, replace with a numeric representation.
Dim itemBool As Boolean
If Boolean.TryParse(item, itemBool)
Console.Write(Convert.ToByte(itemBool))
Else
Console.Write(item)
End If
Next
Console.WriteLine()
lineInput = Console.ReadLine()
End While
End Sub
End Class
備註
的文字表示 value
是藉由呼叫 Single.ToString 方法來產生。
另請參閱
適用於
Write(Char)
- 來源:
- Console.cs
- 來源:
- Console.cs
- 來源:
- Console.cs
將指定的 Unicode 字元值寫入標準輸出資料流。
public:
static void Write(char value);
public static void Write (char value);
static member Write : char -> unit
Public Shared Sub Write (value As Char)
參數
- value
- Char
要寫入的值。
例外狀況
發生 I/O 錯誤。
範例
在下列程式碼中,說明了如何使用 Write
方法。
int main()
{
array<String^>^lineInputArr = {"1 2.2 hello TRUE","2 5.22 bye FALSE","3 6.38 see ya' TRUE"};
for ( Int32 i = 0; i < 3; i++ )
{
String^ lineInput = lineInputArr->GetValue( i )->ToString();
String^ aChar = "\t";
array<String^>^fields = lineInput->Split( aChar->ToCharArray() );
Boolean isFirstField = true;
for ( Int32 i = 0; i < fields->Length; i++ )
{
if ( isFirstField )
isFirstField = false;
else
Console::Write( "," );
// If the field represents a boolean, replace with a numeric representation.
try
{
Console::Write( Convert::ToByte( Convert::ToBoolean( fields[ i ] ) ) );
}
catch ( FormatException^ )
{
Console::Write( fields[ i ] );
}
}
Console::WriteLine();
}
}
public class FormatConverter
{
public static void Main(string[] args)
{
string lineInput;
while ((lineInput = Console.ReadLine()) != null)
{
string[] fields = lineInput.Split(new char[] {'\t'});
bool isFirstField = true;
foreach (var item in fields)
{
if (isFirstField)
isFirstField = false;
else
Console.Write(',');
// If the field represents a boolean, replace with a numeric representation.
bool itemBool;
if (Boolean.TryParse(item, out itemBool))
Console.Write(Convert.ToByte(itemBool));
else
Console.Write(item);
}
Console.WriteLine();
}
}
}
let mutable lineInput = Console.ReadLine()
while lineInput <> null do
let fields = lineInput.Split '\t'
let mutable isFirstField = true
for item in fields do
if isFirstField then
isFirstField <- false
else
Console.Write ','
// If the field represents a boolean, replace with a numeric representation.
match Boolean.TryParse item with
| true, itemBool ->
Console.Write(Convert.ToByte itemBool)
| _ ->
Console.Write item
Console.WriteLine()
Public Class FormatConverter
Public Shared Sub Main()
Dim lineInput As String = Console.ReadLine()
While Not lineInput Is Nothing
Dim fields As String() = lineInput.Split(ControlChars.Tab)
Dim isFirstField As Boolean = True
For Each item As String In fields
If isFirstField Then
isFirstField = False
Else
Console.Write(",")
End If
' If the field represents a boolean, replace with a numeric representation.
Dim itemBool As Boolean
If Boolean.TryParse(item, itemBool)
Console.Write(Convert.ToByte(itemBool))
Else
Console.Write(item)
End If
Next
Console.WriteLine()
lineInput = Console.ReadLine()
End While
End Sub
End Class
另請參閱
適用於
Write(Char[])
- 來源:
- Console.cs
- 來源:
- Console.cs
- 來源:
- Console.cs
將指定的 Unicode 字元陣列寫入標準輸出資料流。
public:
static void Write(cli::array <char> ^ buffer);
public static void Write (char[]? buffer);
public static void Write (char[] buffer);
static member Write : char[] -> unit
Public Shared Sub Write (buffer As Char())
參數
- buffer
- Char[]
Unicode 字元陣列。
例外狀況
發生 I/O 錯誤。
另請參閱
適用於
Write(Boolean)
- 來源:
- Console.cs
- 來源:
- Console.cs
- 來源:
- Console.cs
將指定布林值 (Boolean) 的文字表示寫入標準輸出資料流。
public:
static void Write(bool value);
public static void Write (bool value);
static member Write : bool -> unit
Public Shared Sub Write (value As Boolean)
參數
- value
- Boolean
要寫入的值。
例外狀況
發生 I/O 錯誤。
範例
在下列程式碼中,說明了如何使用 Write
方法。
int main()
{
array<String^>^lineInputArr = {"1 2.2 hello TRUE","2 5.22 bye FALSE","3 6.38 see ya' TRUE"};
for ( Int32 i = 0; i < 3; i++ )
{
String^ lineInput = lineInputArr->GetValue( i )->ToString();
String^ aChar = "\t";
array<String^>^fields = lineInput->Split( aChar->ToCharArray() );
Boolean isFirstField = true;
for ( Int32 i = 0; i < fields->Length; i++ )
{
if ( isFirstField )
isFirstField = false;
else
Console::Write( "," );
// If the field represents a boolean, replace with a numeric representation.
try
{
Console::Write( Convert::ToByte( Convert::ToBoolean( fields[ i ] ) ) );
}
catch ( FormatException^ )
{
Console::Write( fields[ i ] );
}
}
Console::WriteLine();
}
}
public class FormatConverter
{
public static void Main(string[] args)
{
string lineInput;
while ((lineInput = Console.ReadLine()) != null)
{
string[] fields = lineInput.Split(new char[] {'\t'});
bool isFirstField = true;
foreach (var item in fields)
{
if (isFirstField)
isFirstField = false;
else
Console.Write(',');
// If the field represents a boolean, replace with a numeric representation.
bool itemBool;
if (Boolean.TryParse(item, out itemBool))
Console.Write(Convert.ToByte(itemBool));
else
Console.Write(item);
}
Console.WriteLine();
}
}
}
let mutable lineInput = Console.ReadLine()
while lineInput <> null do
let fields = lineInput.Split '\t'
let mutable isFirstField = true
for item in fields do
if isFirstField then
isFirstField <- false
else
Console.Write ','
// If the field represents a boolean, replace with a numeric representation.
match Boolean.TryParse item with
| true, itemBool ->
Console.Write(Convert.ToByte itemBool)
| _ ->
Console.Write item
Console.WriteLine()
Public Class FormatConverter
Public Shared Sub Main()
Dim lineInput As String = Console.ReadLine()
While Not lineInput Is Nothing
Dim fields As String() = lineInput.Split(ControlChars.Tab)
Dim isFirstField As Boolean = True
For Each item As String In fields
If isFirstField Then
isFirstField = False
Else
Console.Write(",")
End If
' If the field represents a boolean, replace with a numeric representation.
Dim itemBool As Boolean
If Boolean.TryParse(item, itemBool)
Console.Write(Convert.ToByte(itemBool))
Else
Console.Write(item)
End If
Next
Console.WriteLine()
lineInput = Console.ReadLine()
End While
End Sub
End Class
備註
的文字表示 value
是藉由呼叫 Boolean.ToString來產生,其會輸出 Boolean.TrueString 或 Boolean.FalseString。
另請參閱
適用於
Write(Double)
- 來源:
- Console.cs
- 來源:
- Console.cs
- 來源:
- Console.cs
將指定的雙精確度浮點數值的文字表示寫入標準輸出資料流。
public:
static void Write(double value);
public static void Write (double value);
static member Write : double -> unit
Public Shared Sub Write (value As Double)
參數
- value
- Double
要寫入的值。
例外狀況
發生 I/O 錯誤。
範例
在下列程式碼中,說明了如何使用 Write
方法。
int main()
{
array<String^>^lineInputArr = {"1 2.2 hello TRUE","2 5.22 bye FALSE","3 6.38 see ya' TRUE"};
for ( Int32 i = 0; i < 3; i++ )
{
String^ lineInput = lineInputArr->GetValue( i )->ToString();
String^ aChar = "\t";
array<String^>^fields = lineInput->Split( aChar->ToCharArray() );
Boolean isFirstField = true;
for ( Int32 i = 0; i < fields->Length; i++ )
{
if ( isFirstField )
isFirstField = false;
else
Console::Write( "," );
// If the field represents a boolean, replace with a numeric representation.
try
{
Console::Write( Convert::ToByte( Convert::ToBoolean( fields[ i ] ) ) );
}
catch ( FormatException^ )
{
Console::Write( fields[ i ] );
}
}
Console::WriteLine();
}
}
public class FormatConverter
{
public static void Main(string[] args)
{
string lineInput;
while ((lineInput = Console.ReadLine()) != null)
{
string[] fields = lineInput.Split(new char[] {'\t'});
bool isFirstField = true;
foreach (var item in fields)
{
if (isFirstField)
isFirstField = false;
else
Console.Write(',');
// If the field represents a boolean, replace with a numeric representation.
bool itemBool;
if (Boolean.TryParse(item, out itemBool))
Console.Write(Convert.ToByte(itemBool));
else
Console.Write(item);
}
Console.WriteLine();
}
}
}
let mutable lineInput = Console.ReadLine()
while lineInput <> null do
let fields = lineInput.Split '\t'
let mutable isFirstField = true
for item in fields do
if isFirstField then
isFirstField <- false
else
Console.Write ','
// If the field represents a boolean, replace with a numeric representation.
match Boolean.TryParse item with
| true, itemBool ->
Console.Write(Convert.ToByte itemBool)
| _ ->
Console.Write item
Console.WriteLine()
Public Class FormatConverter
Public Shared Sub Main()
Dim lineInput As String = Console.ReadLine()
While Not lineInput Is Nothing
Dim fields As String() = lineInput.Split(ControlChars.Tab)
Dim isFirstField As Boolean = True
For Each item As String In fields
If isFirstField Then
isFirstField = False
Else
Console.Write(",")
End If
' If the field represents a boolean, replace with a numeric representation.
Dim itemBool As Boolean
If Boolean.TryParse(item, itemBool)
Console.Write(Convert.ToByte(itemBool))
Else
Console.Write(item)
End If
Next
Console.WriteLine()
lineInput = Console.ReadLine()
End While
End Sub
End Class
備註
的文字表示 value
是藉由呼叫 Double.ToString 方法來產生。
另請參閱
適用於
Write(Int32)
- 來源:
- Console.cs
- 來源:
- Console.cs
- 來源:
- Console.cs
將指定 32 位元帶正負號整數值的文字表示寫入標準輸出資料流。
public:
static void Write(int value);
public static void Write (int value);
static member Write : int -> unit
Public Shared Sub Write (value As Integer)
參數
- value
- Int32
要寫入的值。
例外狀況
發生 I/O 錯誤。
範例
在下列程式碼中,說明了如何使用 Write
方法。
int main()
{
array<String^>^lineInputArr = {"1 2.2 hello TRUE","2 5.22 bye FALSE","3 6.38 see ya' TRUE"};
for ( Int32 i = 0; i < 3; i++ )
{
String^ lineInput = lineInputArr->GetValue( i )->ToString();
String^ aChar = "\t";
array<String^>^fields = lineInput->Split( aChar->ToCharArray() );
Boolean isFirstField = true;
for ( Int32 i = 0; i < fields->Length; i++ )
{
if ( isFirstField )
isFirstField = false;
else
Console::Write( "," );
// If the field represents a boolean, replace with a numeric representation.
try
{
Console::Write( Convert::ToByte( Convert::ToBoolean( fields[ i ] ) ) );
}
catch ( FormatException^ )
{
Console::Write( fields[ i ] );
}
}
Console::WriteLine();
}
}
public class FormatConverter
{
public static void Main(string[] args)
{
string lineInput;
while ((lineInput = Console.ReadLine()) != null)
{
string[] fields = lineInput.Split(new char[] {'\t'});
bool isFirstField = true;
foreach (var item in fields)
{
if (isFirstField)
isFirstField = false;
else
Console.Write(',');
// If the field represents a boolean, replace with a numeric representation.
bool itemBool;
if (Boolean.TryParse(item, out itemBool))
Console.Write(Convert.ToByte(itemBool));
else
Console.Write(item);
}
Console.WriteLine();
}
}
}
let mutable lineInput = Console.ReadLine()
while lineInput <> null do
let fields = lineInput.Split '\t'
let mutable isFirstField = true
for item in fields do
if isFirstField then
isFirstField <- false
else
Console.Write ','
// If the field represents a boolean, replace with a numeric representation.
match Boolean.TryParse item with
| true, itemBool ->
Console.Write(Convert.ToByte itemBool)
| _ ->
Console.Write item
Console.WriteLine()
Public Class FormatConverter
Public Shared Sub Main()
Dim lineInput As String = Console.ReadLine()
While Not lineInput Is Nothing
Dim fields As String() = lineInput.Split(ControlChars.Tab)
Dim isFirstField As Boolean = True
For Each item As String In fields
If isFirstField Then
isFirstField = False
Else
Console.Write(",")
End If
' If the field represents a boolean, replace with a numeric representation.
Dim itemBool As Boolean
If Boolean.TryParse(item, itemBool)
Console.Write(Convert.ToByte(itemBool))
Else
Console.Write(item)
End If
Next
Console.WriteLine()
lineInput = Console.ReadLine()
End While
End Sub
End Class
備註
的文字表示 value
是藉由呼叫 Int32.ToString 方法來產生。
另請參閱
適用於
Write(Int64)
- 來源:
- Console.cs
- 來源:
- Console.cs
- 來源:
- Console.cs
將指定 64 位元帶正負號整數值的文字表示寫入標準輸出資料流。
public:
static void Write(long value);
public static void Write (long value);
static member Write : int64 -> unit
Public Shared Sub Write (value As Long)
參數
- value
- Int64
要寫入的值。
例外狀況
發生 I/O 錯誤。
範例
在下列程式碼中,說明了如何使用 Write
方法。
int main()
{
array<String^>^lineInputArr = {"1 2.2 hello TRUE","2 5.22 bye FALSE","3 6.38 see ya' TRUE"};
for ( Int32 i = 0; i < 3; i++ )
{
String^ lineInput = lineInputArr->GetValue( i )->ToString();
String^ aChar = "\t";
array<String^>^fields = lineInput->Split( aChar->ToCharArray() );
Boolean isFirstField = true;
for ( Int32 i = 0; i < fields->Length; i++ )
{
if ( isFirstField )
isFirstField = false;
else
Console::Write( "," );
// If the field represents a boolean, replace with a numeric representation.
try
{
Console::Write( Convert::ToByte( Convert::ToBoolean( fields[ i ] ) ) );
}
catch ( FormatException^ )
{
Console::Write( fields[ i ] );
}
}
Console::WriteLine();
}
}
public class FormatConverter
{
public static void Main(string[] args)
{
string lineInput;
while ((lineInput = Console.ReadLine()) != null)
{
string[] fields = lineInput.Split(new char[] {'\t'});
bool isFirstField = true;
foreach (var item in fields)
{
if (isFirstField)
isFirstField = false;
else
Console.Write(',');
// If the field represents a boolean, replace with a numeric representation.
bool itemBool;
if (Boolean.TryParse(item, out itemBool))
Console.Write(Convert.ToByte(itemBool));
else
Console.Write(item);
}
Console.WriteLine();
}
}
}
let mutable lineInput = Console.ReadLine()
while lineInput <> null do
let fields = lineInput.Split '\t'
let mutable isFirstField = true
for item in fields do
if isFirstField then
isFirstField <- false
else
Console.Write ','
// If the field represents a boolean, replace with a numeric representation.
match Boolean.TryParse item with
| true, itemBool ->
Console.Write(Convert.ToByte itemBool)
| _ ->
Console.Write item
Console.WriteLine()
Public Class FormatConverter
Public Shared Sub Main()
Dim lineInput As String = Console.ReadLine()
While Not lineInput Is Nothing
Dim fields As String() = lineInput.Split(ControlChars.Tab)
Dim isFirstField As Boolean = True
For Each item As String In fields
If isFirstField Then
isFirstField = False
Else
Console.Write(",")
End If
' If the field represents a boolean, replace with a numeric representation.
Dim itemBool As Boolean
If Boolean.TryParse(item, itemBool)
Console.Write(Convert.ToByte(itemBool))
Else
Console.Write(item)
End If
Next
Console.WriteLine()
lineInput = Console.ReadLine()
End While
End Sub
End Class
備註
的文字表示 value
是藉由呼叫 Int64.ToString 方法來產生。
另請參閱
適用於
Write(Decimal)
- 來源:
- Console.cs
- 來源:
- Console.cs
- 來源:
- Console.cs
將指定 Decimal 值的表示文字寫入標準輸出資料流。
public:
static void Write(System::Decimal value);
public static void Write (decimal value);
static member Write : decimal -> unit
Public Shared Sub Write (value As Decimal)
參數
- value
- Decimal
要寫入的值。
例外狀況
發生 I/O 錯誤。
範例
在下列程式碼中,說明了如何使用 Write
方法。
int main()
{
array<String^>^lineInputArr = {"1 2.2 hello TRUE","2 5.22 bye FALSE","3 6.38 see ya' TRUE"};
for ( Int32 i = 0; i < 3; i++ )
{
String^ lineInput = lineInputArr->GetValue( i )->ToString();
String^ aChar = "\t";
array<String^>^fields = lineInput->Split( aChar->ToCharArray() );
Boolean isFirstField = true;
for ( Int32 i = 0; i < fields->Length; i++ )
{
if ( isFirstField )
isFirstField = false;
else
Console::Write( "," );
// If the field represents a boolean, replace with a numeric representation.
try
{
Console::Write( Convert::ToByte( Convert::ToBoolean( fields[ i ] ) ) );
}
catch ( FormatException^ )
{
Console::Write( fields[ i ] );
}
}
Console::WriteLine();
}
}
public class FormatConverter
{
public static void Main(string[] args)
{
string lineInput;
while ((lineInput = Console.ReadLine()) != null)
{
string[] fields = lineInput.Split(new char[] {'\t'});
bool isFirstField = true;
foreach (var item in fields)
{
if (isFirstField)
isFirstField = false;
else
Console.Write(',');
// If the field represents a boolean, replace with a numeric representation.
bool itemBool;
if (Boolean.TryParse(item, out itemBool))
Console.Write(Convert.ToByte(itemBool));
else
Console.Write(item);
}
Console.WriteLine();
}
}
}
let mutable lineInput = Console.ReadLine()
while lineInput <> null do
let fields = lineInput.Split '\t'
let mutable isFirstField = true
for item in fields do
if isFirstField then
isFirstField <- false
else
Console.Write ','
// If the field represents a boolean, replace with a numeric representation.
match Boolean.TryParse item with
| true, itemBool ->
Console.Write(Convert.ToByte itemBool)
| _ ->
Console.Write item
Console.WriteLine()
Public Class FormatConverter
Public Shared Sub Main()
Dim lineInput As String = Console.ReadLine()
While Not lineInput Is Nothing
Dim fields As String() = lineInput.Split(ControlChars.Tab)
Dim isFirstField As Boolean = True
For Each item As String In fields
If isFirstField Then
isFirstField = False
Else
Console.Write(",")
End If
' If the field represents a boolean, replace with a numeric representation.
Dim itemBool As Boolean
If Boolean.TryParse(item, itemBool)
Console.Write(Convert.ToByte(itemBool))
Else
Console.Write(item)
End If
Next
Console.WriteLine()
lineInput = Console.ReadLine()
End While
End Sub
End Class
備註
的文字表示 value
是藉由呼叫 Decimal.ToString來產生。
另請參閱
適用於
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應