Console.Write Metode
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Menulis representasi teks dari nilai atau nilai yang ditentukan ke aliran output standar.
Overload
Write(String, Object, Object, Object, Object) |
Menulis representasi teks dari objek yang ditentukan dan daftar parameter panjang variabel ke aliran output standar menggunakan informasi format yang ditentukan. |
Write(String, Object, Object, Object) |
Menulis representasi teks dari objek yang ditentukan ke aliran output standar menggunakan informasi format yang ditentukan. |
Write(Char[], Int32, Int32) |
Menulis subarray karakter Unicode yang ditentukan ke aliran output standar. |
Write(String, ReadOnlySpan<Object>) |
Menulis representasi teks dari rentang objek yang ditentukan ke aliran output standar menggunakan informasi format yang ditentukan. |
Write(String, Object[]) |
Menulis representasi teks dari array objek yang ditentukan ke aliran output standar menggunakan informasi format yang ditentukan. |
Write(String, Object) |
Menulis representasi teks dari objek yang ditentukan ke aliran output standar menggunakan informasi format yang ditentukan. |
Write(UInt64) |
Menulis representasi teks dari nilai bilangan bulat yang tidak ditandatangani 64-bit yang ditentukan ke aliran output standar. |
Write(UInt32) |
Menulis representasi teks dari nilai bilangan bulat yang tidak ditandatangani 32-bit yang ditentukan ke aliran output standar. |
Write(String) |
Menulis nilai string yang ditentukan ke aliran output standar. |
Write(String, Object, Object) |
Menulis representasi teks dari objek yang ditentukan ke aliran output standar menggunakan informasi format yang ditentukan. |
Write(Object) |
Menulis representasi teks dari objek yang ditentukan ke aliran output standar. |
Write(Single) |
Menulis representasi teks dari nilai floating-point presisi tunggal yang ditentukan ke aliran output standar. |
Write(Char) |
Menulis nilai karakter Unicode yang ditentukan ke aliran output standar. |
Write(Char[]) |
Menulis array karakter Unicode yang ditentukan ke aliran output standar. |
Write(Boolean) |
Menulis representasi teks dari nilai Boolean yang ditentukan ke aliran output standar. |
Write(Double) |
Menulis representasi teks dari nilai floating-point presisi ganda yang ditentukan ke aliran output standar. |
Write(Int32) |
Menulis representasi teks dari nilai bilangan bulat bertanda tangan 32-bit yang ditentukan ke aliran output standar. |
Write(Int64) |
Menulis representasi teks dari nilai bilangan bulat bertanda 64-bit yang ditentukan ke aliran output standar. |
Write(Decimal) |
Menulis representasi teks dari nilai Decimal yang ditentukan ke aliran output standar. |
Write(String, Object, Object, Object, Object)
Penting
API ini bukan kompatibel CLS.
Menulis representasi teks dari objek yang ditentukan dan daftar parameter panjang variabel ke aliran output standar menggunakan informasi format yang ditentukan.
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)
Parameter
- format
- String
String format komposit.
- arg0
- Object
Objek pertama yang ditulis menggunakan format
.
- arg1
- Object
Objek kedua yang ditulis menggunakan format
.
- arg2
- Object
Objek ketiga yang ditulis menggunakan format
.
- arg3
- Object
Objek keempat yang ditulis menggunakan format
.
- Atribut
Pengecualian
Terjadi kesalahan I/O.
format
null
.
Spesifikasi format dalam format
tidak valid.
Contoh
Contoh berikut mengilustrasikan penggunaan parameter variabel dengan metode Write(String, Object, Object, Object, Object). Metode ini dipanggil dengan string format komposit dan lima item format.
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
Keterangan
Nota
API ini tidak sesuai dengan CLS. Alternatif yang mematuhi CLS adalah Console.Write(String, Object[]). Pengkompilasi C# dan Visual Basic secara otomatis menyelesaikan panggilan ke metode ini sebagai panggilan ke Console.Write(String, Object[]).
Metode ini menggunakan fitur pemformatan komposit .NET untuk mengonversi nilai objek ke representasi teksnya dan menyematkan representasi tersebut dalam string. String yang dihasilkan ditulis ke aliran output.
Parameter format
terdiri dari nol atau lebih eksekusi teks yang diindeks dengan nol atau lebih tempat penampung terindeks, yang disebut item format, yang sesuai dengan objek dalam daftar parameter metode ini. Proses pemformatan menggantikan setiap item format dengan representasi teks dari nilai objek yang sesuai.
Sintaks item format {
indeks[,
perataan][:
formatString]}
, yang menentukan indeks wajib, panjang opsional dan perataan teks yang diformat, dan string opsional karakter penentu format yang mengatur bagaimana nilai objek yang sesuai diformat.
.NET menyediakan dukungan pemformatan ekstensif, yang dijelaskan secara lebih rinci dalam topik pemformatan berikut.
Untuk informasi selengkapnya tentang fitur pemformatan komposit yang didukung oleh metode seperti Format, AppendFormat, dan beberapa kelebihan beban WriteLine, lihat Pemformatan Komposit.
Untuk informasi selengkapnya tentang penentu format numerik, lihat String Format Numerik Standar dan String Format Numerik Kustom.
Untuk informasi selengkapnya tentang penentu format tanggal dan waktu, lihat String Format Tanggal dan Waktu Standar dan String Format Tanggal dan Waktu Kustom.
Untuk informasi selengkapnya tentang penentu format enumerasi, lihat String Format Enumerasi.
Untuk informasi selengkapnya tentang pemformatan, lihat Tipe Pemformatan.
Catatan Bagi Pemanggil
Metode ini ditandai dengan kata kunci vararg
, yang berarti mendukung jumlah parameter variabel. Metode ini dapat dipanggil dari Visual C++, tetapi tidak dapat dipanggil dari kode C# atau Visual Basic. Pengkompilasi C# dan Visual Basic menyelesaikan panggilan ke Write(String, Object, Object, Object, Object) sebagai panggilan ke Write(String, Object[]).
Lihat juga
- Read()
- ReadLine()
- WriteLine()
- Tipe Pemformatan di .NET
- Pemformatan Komposit
Berlaku untuk
Write(String, Object, Object, Object)
- Sumber:
- Console.cs
- Sumber:
- Console.cs
- Sumber:
- Console.cs
Menulis representasi teks dari objek yang ditentukan ke aliran output standar menggunakan informasi format yang ditentukan.
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)
Parameter
- format
- String
String format komposit.
- arg0
- Object
Objek pertama yang ditulis menggunakan format
.
- arg1
- Object
Objek kedua yang ditulis menggunakan format
.
- arg2
- Object
Objek ketiga yang ditulis menggunakan format
.
Pengecualian
Terjadi kesalahan I/O.
format
null
.
Spesifikasi format dalam format
tidak valid.
Contoh
Contoh berikut menggunakan metode WriteLine
untuk menunjukkan penentu pemformatan standar untuk angka, tanggal, dan enumerasi.
// 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
'
Contoh berikut mengilustrasikan penggunaan metode 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
Keterangan
Metode ini menggunakan fitur pemformatan komposit .NET untuk mengonversi nilai objek ke representasi teksnya dan menyematkan representasi tersebut dalam string. String yang dihasilkan ditulis ke aliran output.
Parameter format
terdiri dari nol atau lebih eksekusi teks yang diindeks dengan nol atau lebih tempat penampung terindeks, yang disebut item format, yang sesuai dengan objek dalam daftar parameter metode ini. Proses pemformatan menggantikan setiap item format dengan representasi teks dari nilai objek yang sesuai.
Sintaks item format {
indeks[,
perataan][:
formatString]}
, yang menentukan indeks wajib, panjang opsional dan perataan teks yang diformat, dan string opsional karakter penentu format yang mengatur bagaimana nilai objek yang sesuai diformat.
.NET menyediakan dukungan pemformatan ekstensif, yang dijelaskan secara lebih rinci dalam topik pemformatan berikut.
Untuk informasi selengkapnya tentang fitur pemformatan komposit yang didukung oleh metode seperti Format, AppendFormat, dan beberapa kelebihan beban WriteLine, lihat Pemformatan Komposit.
Untuk informasi selengkapnya tentang penentu format numerik, lihat String Format Numerik Standar dan String Format Numerik Kustom.
Untuk informasi selengkapnya tentang penentu format tanggal dan waktu, lihat String Format Tanggal dan Waktu Standar dan String Format Tanggal dan Waktu Kustom.
Untuk informasi selengkapnya tentang penentu format enumerasi, lihat String Format Enumerasi.
Untuk informasi selengkapnya tentang pemformatan, lihat Tipe Pemformatan.
Lihat juga
- Read()
- ReadLine()
- WriteLine()
- Tipe Pemformatan di .NET
- Pemformatan Komposit
Berlaku untuk
Write(Char[], Int32, Int32)
- Sumber:
- Console.cs
- Sumber:
- Console.cs
- Sumber:
- Console.cs
Menulis subarray karakter Unicode yang ditentukan ke aliran output standar.
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)
Parameter
- buffer
- Char[]
Array karakter Unicode.
- index
- Int32
Posisi awal di buffer
.
- count
- Int32
Jumlah karakter yang akan ditulis.
Pengecualian
buffer
null
.
index
atau count
kurang dari nol.
index
plus count
menentukan posisi yang tidak berada dalam buffer
.
Terjadi kesalahan I/O.
Keterangan
Metode ini menulis count
karakter mulai dari index
posisi buffer
ke aliran output standar.
Lihat juga
Berlaku untuk
Write(String, ReadOnlySpan<Object>)
Menulis representasi teks dari rentang objek yang ditentukan ke aliran output standar menggunakan informasi format yang ditentukan.
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))
Parameter
- format
- String
String format komposit.
- arg
- ReadOnlySpan<Object>
Rentang objek untuk ditulis menggunakan format.
Berlaku untuk
Write(String, Object[])
- Sumber:
- Console.cs
- Sumber:
- Console.cs
- Sumber:
- Console.cs
Menulis representasi teks dari array objek yang ditentukan ke aliran output standar menggunakan informasi format yang ditentukan.
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())
Parameter
- format
- String
String format komposit.
- arg
- Object[]
Array objek untuk ditulis menggunakan format
.
Pengecualian
Terjadi kesalahan I/O.
format
atau arg
null
.
Spesifikasi format dalam format
tidak valid.
Contoh
Contoh berikut mendefinisikan kelas Person
dengan sejumlah properti yang menyediakan informasi tentang seseorang. Metode GetDescription
mengembalikan array yang berisi semua nilai properti kecuali satu. Contoh kemudian menggunakan array yang dikembalikan oleh metode GetDescription
untuk menampilkan nilai objek Orang.
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
Perhatikan bahwa contoh memanggil metode Write(String, Object[]) daripada metode WriteLine(String, Object[]) karena mencoba menampilkan nilai properti Person.Remarks
pada baris yang sama. Untuk melakukan ini, ini memeriksa nilai properti CursorLeft dan WindowWidth untuk menentukan apakah ada cukup ruang agar sesuai dengan keterangan. Jika ada, baris akan ditampilkan. Jika tidak, ia menulis baris, menginden tiga spasi, dan menampilkan komentar.
Contoh berikut identik dengan yang pertama, kecuali bahwa ia menyediakan daftar lima item sebagai argumen arg
alih-alih array parameter.
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
Keterangan
Metode ini menggunakan fitur pemformatan komposit .NET untuk mengonversi nilai objek ke representasi teksnya dan menyematkan representasi tersebut dalam string. String yang dihasilkan ditulis ke aliran output.
Parameter format
terdiri dari nol atau lebih eksekusi teks yang diindeks dengan nol atau lebih tempat penampung terindeks, yang disebut item format, yang sesuai dengan objek dalam daftar parameter metode ini. Proses pemformatan menggantikan setiap item format dengan representasi teks dari nilai objek yang sesuai.
Sintaks item format {
indeks[,
perataan][:
formatString]}
, yang menentukan indeks wajib, panjang opsional dan perataan teks yang diformat, dan string opsional karakter penentu format yang mengatur bagaimana nilai objek yang sesuai diformat.
.NET menyediakan dukungan pemformatan ekstensif, yang dijelaskan secara lebih rinci dalam topik pemformatan berikut.
Untuk informasi selengkapnya tentang fitur pemformatan komposit yang didukung oleh metode seperti Format, AppendFormat, dan beberapa kelebihan beban WriteLine, lihat Pemformatan Komposit.
Untuk informasi selengkapnya tentang penentu format numerik, lihat String Format Numerik Standar dan String Format Numerik Kustom.
Untuk informasi selengkapnya tentang penentu format tanggal dan waktu, lihat String Format Tanggal dan Waktu Standar dan String Format Tanggal dan Waktu Kustom.
Untuk informasi selengkapnya tentang penentu format enumerasi, lihat String Format Enumerasi.
Untuk informasi selengkapnya tentang pemformatan, lihat Tipe Pemformatan.
Parameter arg
adalah array parameter. Argumen dapat diteruskan ke metode baik sebagai array atau sebagai daftar lima item atau lebih. Contoh mengilustrasikan kedua bentuk panggilan metode.
Catatan Bagi Pemanggil
Metode ini tidak dipanggil oleh kode C++. Pengkompilasi C++ menyelesaikan panggilan ke System.Console.Write yang menyertakan string dan daftar empat parameter objek atau lebih sebagai panggilan ke Write(String, Object, Object, Object, Object). Ini menyelesaikan panggilan ke System.Console.Write yang menyertakan string dan array objek sebagai panggilan ke Write(String, Object).
Lihat juga
- Read()
- ReadLine()
- WriteLine()
- Tipe Pemformatan di .NET
- Pemformatan Komposit
Berlaku untuk
Write(String, Object)
- Sumber:
- Console.cs
- Sumber:
- Console.cs
- Sumber:
- Console.cs
Menulis representasi teks dari objek yang ditentukan ke aliran output standar menggunakan informasi format yang ditentukan.
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)
Parameter
- format
- String
String format komposit.
- arg0
- Object
Objek untuk ditulis menggunakan format
.
Pengecualian
Terjadi kesalahan I/O.
format
null
.
Spesifikasi format dalam format
tidak valid.
Contoh
Contoh berikut menggunakan metode WriteLine
untuk menunjukkan penentu pemformatan standar untuk angka, tanggal, dan enumerasi.
// 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
'
Contoh berikut mengilustrasikan penggunaan metode 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
Keterangan
Metode ini menggunakan fitur pemformatan komposit dari .NET untuk mengonversi nilai objek ke representasi teksnya dan menyematkan representasi tersebut dalam string. String yang dihasilkan ditulis ke aliran output.
Parameter format
terdiri dari nol atau lebih eksekusi teks yang diindeks dengan nol atau lebih tempat penampung terindeks, yang disebut item format, yang sesuai dengan objek dalam daftar parameter metode ini. Proses pemformatan menggantikan setiap item format dengan representasi teks dari nilai objek yang sesuai.
Sintaks item format {
indeks[,
perataan][:
formatString]}
, yang menentukan indeks wajib, panjang opsional dan perataan teks yang diformat, dan string opsional karakter penentu format yang mengatur bagaimana nilai objek yang sesuai diformat.
.NET menyediakan dukungan pemformatan ekstensif, yang dijelaskan secara lebih rinci dalam topik pemformatan berikut.
Untuk informasi selengkapnya tentang fitur pemformatan komposit yang didukung oleh metode seperti Format, AppendFormat, dan beberapa kelebihan beban WriteLine, lihat Pemformatan Komposit.
Untuk informasi selengkapnya tentang penentu format numerik, lihat String Format Numerik Standar dan String Format Numerik Kustom.
Untuk informasi selengkapnya tentang penentu format tanggal dan waktu, lihat String Format Tanggal dan Waktu Standar dan String Format Tanggal dan Waktu Kustom.
Untuk informasi selengkapnya tentang penentu format enumerasi, lihat String Format Enumerasi.
Untuk informasi selengkapnya tentang pemformatan, lihat Tipe Pemformatan.
Lihat juga
- Read()
- ReadLine()
- WriteLine()
- Tipe Pemformatan di .NET
- Pemformatan Komposit
Berlaku untuk
Write(UInt64)
- Sumber:
- Console.cs
- Sumber:
- Console.cs
- Sumber:
- Console.cs
Penting
API ini bukan kompatibel CLS.
Menulis representasi teks dari nilai bilangan bulat yang tidak ditandatangani 64-bit yang ditentukan ke aliran output standar.
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)
Parameter
- value
- UInt64
Nilai yang akan ditulis.
- Atribut
Pengecualian
Terjadi kesalahan I/O.
Contoh
Contoh berikut mengilustrasikan penggunaan metode 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
Keterangan
Representasi teks value
diproduksi dengan memanggil metode UInt64.ToString.
Lihat juga
Berlaku untuk
Write(UInt32)
- Sumber:
- Console.cs
- Sumber:
- Console.cs
- Sumber:
- Console.cs
Penting
API ini bukan kompatibel CLS.
Menulis representasi teks dari nilai bilangan bulat yang tidak ditandatangani 32-bit yang ditentukan ke aliran output standar.
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)
Parameter
- value
- UInt32
Nilai yang akan ditulis.
- Atribut
Pengecualian
Terjadi kesalahan I/O.
Contoh
Contoh berikut mengilustrasikan penggunaan metode 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
Keterangan
Representasi teks value
diproduksi dengan memanggil metode UInt32.ToString.
Lihat juga
Berlaku untuk
Write(String)
- Sumber:
- Console.cs
- Sumber:
- Console.cs
- Sumber:
- Console.cs
Menulis nilai string yang ditentukan ke aliran output standar.
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)
Parameter
- value
- String
Nilai yang akan ditulis.
Pengecualian
Terjadi kesalahan I/O.
Contoh
Contoh berikut mengilustrasikan penggunaan metode 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
Keterangan
Jika nilai null
, tidak ada yang ditulis ke aliran output standar.
Lihat juga
Berlaku untuk
Write(String, Object, Object)
- Sumber:
- Console.cs
- Sumber:
- Console.cs
- Sumber:
- Console.cs
Menulis representasi teks dari objek yang ditentukan ke aliran output standar menggunakan informasi format yang ditentukan.
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)
Parameter
- format
- String
String format komposit.
- arg0
- Object
Objek pertama yang ditulis menggunakan format
.
- arg1
- Object
Objek kedua yang ditulis menggunakan format
.
Pengecualian
Terjadi kesalahan I/O.
format
null
.
Spesifikasi format dalam format
tidak valid.
Contoh
Contoh berikut menggunakan metode WriteLine
untuk menunjukkan penentu pemformatan standar untuk angka, tanggal, dan enumerasi.
// 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
'
Contoh berikut mengilustrasikan penggunaan metode 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
Keterangan
Metode ini menggunakan fitur pemformatan komposit .NET untuk mengonversi nilai objek ke representasi teksnya dan menyematkan representasi tersebut dalam string. String yang dihasilkan ditulis ke aliran output.
Parameter format
terdiri dari nol atau lebih eksekusi teks yang diindeks dengan nol atau lebih tempat penampung terindeks, yang disebut item format, yang sesuai dengan objek dalam daftar parameter metode ini. Proses pemformatan menggantikan setiap item format dengan representasi teks dari nilai objek yang sesuai.
Sintaks item format {
indeks[,
perataan][:
formatString]}
, yang menentukan indeks wajib, panjang opsional dan perataan teks yang diformat, dan string opsional karakter penentu format yang mengatur bagaimana nilai objek yang sesuai diformat.
.NET menyediakan dukungan pemformatan ekstensif, yang dijelaskan secara lebih rinci dalam topik pemformatan berikut.
Untuk informasi selengkapnya tentang fitur pemformatan komposit yang didukung oleh metode seperti Format, AppendFormat, dan beberapa kelebihan beban WriteLine, lihat Pemformatan Komposit.
Untuk informasi selengkapnya tentang penentu format numerik, lihat String Format Numerik Standar dan String Format Numerik Kustom.
Untuk informasi selengkapnya tentang penentu format tanggal dan waktu, lihat String Format Tanggal dan Waktu Standar dan String Format Tanggal dan Waktu Kustom.
Untuk informasi selengkapnya tentang penentu format enumerasi, lihat String Format Enumerasi.
Untuk informasi selengkapnya tentang pemformatan, lihat Tipe Pemformatan.
Lihat juga
- Read()
- ReadLine()
- WriteLine()
- Tipe Pemformatan di .NET
- Pemformatan Komposit
Berlaku untuk
Write(Object)
- Sumber:
- Console.cs
- Sumber:
- Console.cs
- Sumber:
- Console.cs
Menulis representasi teks dari objek yang ditentukan ke aliran output standar.
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)
Parameter
- value
- Object
Nilai untuk menulis, atau null
.
Pengecualian
Terjadi kesalahan I/O.
Contoh
Contoh berikut mengilustrasikan penggunaan metode 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
Keterangan
Jika value
null
, tidak ada yang ditulis dan tidak ada pengecualian yang dilemparkan. Jika tidak, metode ToString
value
dipanggil untuk menghasilkan representasi stringnya, dan string yang dihasilkan ditulis ke aliran output standar.
Lihat juga
Berlaku untuk
Write(Single)
- Sumber:
- Console.cs
- Sumber:
- Console.cs
- Sumber:
- Console.cs
Menulis representasi teks dari nilai floating-point presisi tunggal yang ditentukan ke aliran output standar.
public:
static void Write(float value);
public static void Write (float value);
static member Write : single -> unit
Public Shared Sub Write (value As Single)
Parameter
- value
- Single
Nilai yang akan ditulis.
Pengecualian
Terjadi kesalahan I/O.
Contoh
Contoh berikut mengilustrasikan penggunaan metode 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
Keterangan
Representasi teks value
diproduksi dengan memanggil metode Single.ToString.
Lihat juga
Berlaku untuk
Write(Char)
- Sumber:
- Console.cs
- Sumber:
- Console.cs
- Sumber:
- Console.cs
Menulis nilai karakter Unicode yang ditentukan ke aliran output standar.
public:
static void Write(char value);
public static void Write (char value);
static member Write : char -> unit
Public Shared Sub Write (value As Char)
Parameter
- value
- Char
Nilai yang akan ditulis.
Pengecualian
Terjadi kesalahan I/O.
Contoh
Contoh berikut mengilustrasikan penggunaan metode 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
Lihat juga
Berlaku untuk
Write(Char[])
- Sumber:
- Console.cs
- Sumber:
- Console.cs
- Sumber:
- Console.cs
Menulis array karakter Unicode yang ditentukan ke aliran output standar.
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())
Parameter
- buffer
- Char[]
Array karakter Unicode.
Pengecualian
Terjadi kesalahan I/O.
Lihat juga
Berlaku untuk
Write(Boolean)
- Sumber:
- Console.cs
- Sumber:
- Console.cs
- Sumber:
- Console.cs
Menulis representasi teks dari nilai Boolean yang ditentukan ke aliran output standar.
public:
static void Write(bool value);
public static void Write (bool value);
static member Write : bool -> unit
Public Shared Sub Write (value As Boolean)
Parameter
- value
- Boolean
Nilai yang akan ditulis.
Pengecualian
Terjadi kesalahan I/O.
Contoh
Contoh berikut mengilustrasikan penggunaan metode 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
Keterangan
Representasi teks value
diproduksi dengan memanggil Boolean.ToString, yang menghasilkan Boolean.TrueString atau Boolean.FalseString.
Lihat juga
Berlaku untuk
Write(Double)
- Sumber:
- Console.cs
- Sumber:
- Console.cs
- Sumber:
- Console.cs
Menulis representasi teks dari nilai floating-point presisi ganda yang ditentukan ke aliran output standar.
public:
static void Write(double value);
public static void Write (double value);
static member Write : double -> unit
Public Shared Sub Write (value As Double)
Parameter
- value
- Double
Nilai yang akan ditulis.
Pengecualian
Terjadi kesalahan I/O.
Contoh
Contoh berikut mengilustrasikan penggunaan metode 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
Keterangan
Representasi teks value
diproduksi dengan memanggil metode Double.ToString.
Lihat juga
Berlaku untuk
Write(Int32)
- Sumber:
- Console.cs
- Sumber:
- Console.cs
- Sumber:
- Console.cs
Menulis representasi teks dari nilai bilangan bulat bertanda tangan 32-bit yang ditentukan ke aliran output standar.
public:
static void Write(int value);
public static void Write (int value);
static member Write : int -> unit
Public Shared Sub Write (value As Integer)
Parameter
- value
- Int32
Nilai yang akan ditulis.
Pengecualian
Terjadi kesalahan I/O.
Contoh
Contoh berikut mengilustrasikan penggunaan metode 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
Keterangan
Representasi teks value
diproduksi dengan memanggil metode Int32.ToString.
Lihat juga
Berlaku untuk
Write(Int64)
- Sumber:
- Console.cs
- Sumber:
- Console.cs
- Sumber:
- Console.cs
Menulis representasi teks dari nilai bilangan bulat bertanda 64-bit yang ditentukan ke aliran output standar.
public:
static void Write(long value);
public static void Write (long value);
static member Write : int64 -> unit
Public Shared Sub Write (value As Long)
Parameter
- value
- Int64
Nilai yang akan ditulis.
Pengecualian
Terjadi kesalahan I/O.
Contoh
Contoh berikut mengilustrasikan penggunaan metode 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
Keterangan
Representasi teks value
diproduksi dengan memanggil metode Int64.ToString.
Lihat juga
Berlaku untuk
Write(Decimal)
- Sumber:
- Console.cs
- Sumber:
- Console.cs
- Sumber:
- Console.cs
Menulis representasi teks dari nilai Decimal yang ditentukan ke aliran output standar.
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)
Parameter
- value
- Decimal
Nilai yang akan ditulis.
Pengecualian
Terjadi kesalahan I/O.
Contoh
Contoh berikut mengilustrasikan penggunaan metode 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
Keterangan
Representasi teks value
diproduksi dengan memanggil Decimal.ToString.