StringWriter 類別

定義

實作 TextWriter以將資訊寫入字串。 資訊會儲存在基礎 StringBuilder 中。

public ref class StringWriter : System::IO::TextWriter
public class StringWriter : System.IO.TextWriter
[System.Serializable]
public class StringWriter : System.IO.TextWriter
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class StringWriter : System.IO.TextWriter
type StringWriter = class
    inherit TextWriter
[<System.Serializable>]
type StringWriter = class
    inherit TextWriter
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type StringWriter = class
    inherit TextWriter
Public Class StringWriter
Inherits TextWriter
繼承
StringWriter
繼承
屬性

範例

下列程式碼範例示範如何從一組雙空格句子建立連續段落,然後將段落轉換成原始文字。

using namespace System;
using namespace System::IO;
int main()
{
   String^ textReaderText = "TextReader is the abstract base "
   "class of StreamReader and StringReader, which read "
   "characters from streams and strings, respectively.\n\n"
   "Create an instance of TextReader to open a text file "
   "for reading a specified range of characters, or to "
   "create a reader based on an existing stream.\n\n"
   "You can also use an instance of TextReader to read "
   "text from a custom backing store using the same "
   "APIs you would use for a string or a stream.\n\n";
   Console::WriteLine(  "Original text:\n\n{0}", textReaderText );

   // From textReaderText, create a continuous paragraph 
   // with two spaces between each sentence.
      String^ aLine;
   String^ aParagraph;
   StringReader^ strReader = gcnew StringReader( textReaderText );
   while ( true )
   {
      aLine = strReader->ReadLine();
      if ( aLine != nullptr )
      {
         aParagraph = String::Concat( aParagraph, aLine,  " " );
      }
      else
      {
         aParagraph = String::Concat( aParagraph,  "\n" );
         break;
      }
   }

   Console::WriteLine(  "Modified text:\n\n{0}", aParagraph );
   
   // Re-create textReaderText from aParagraph.
   int intCharacter;
   Char convertedCharacter;
   StringWriter^ strWriter = gcnew StringWriter;
   strReader = gcnew StringReader( aParagraph );
   while ( true )
   {
      intCharacter = strReader->Read();
      
      // Check for the end of the string 
      // before converting to a character.
      if ( intCharacter == -1 )
            break;

      
      convertedCharacter = Convert::ToChar( intCharacter );
      if ( convertedCharacter == '.' )
      {
         strWriter->Write(  ".\n\n" );
         
         // Bypass the spaces between sentences.
         strReader->Read();
         strReader->Read();
      }
      else
      {
         strWriter->Write( convertedCharacter );
      }
   }

   Console::WriteLine(  "\nOriginal text:\n\n{0}", strWriter->ToString() );
}
using System;
using System.IO;

class StringRW
{
    static void Main()
    {
        string textReaderText = "TextReader is the abstract base " +
            "class of StreamReader and StringReader, which read " +
            "characters from streams and strings, respectively.\n\n" +

            "Create an instance of TextReader to open a text file " +
            "for reading a specified range of characters, or to " +
            "create a reader based on an existing stream.\n\n" +

            "You can also use an instance of TextReader to read " +
            "text from a custom backing store using the same " +
            "APIs you would use for a string or a stream.\n\n";

        Console.WriteLine("Original text:\n\n{0}", textReaderText);

        // From textReaderText, create a continuous paragraph
        // with two spaces between each sentence.
        string aLine, aParagraph = null;
        StringReader strReader = new StringReader(textReaderText);
        while(true)
        {
            aLine = strReader.ReadLine();
            if(aLine != null)
            {
                aParagraph = aParagraph + aLine + " ";
            }
            else
            {
                aParagraph = aParagraph + "\n";
                break;
            }
        }
        Console.WriteLine("Modified text:\n\n{0}", aParagraph);

        // Re-create textReaderText from aParagraph.
        int intCharacter;
        char convertedCharacter;
        StringWriter strWriter = new StringWriter();
        strReader = new StringReader(aParagraph);
        while(true)
        {
            intCharacter = strReader.Read();

            // Check for the end of the string
            // before converting to a character.
            if(intCharacter == -1) break;

            convertedCharacter = (char)intCharacter;
            if(convertedCharacter == '.')
            {
                strWriter.Write(".\n\n");

                // Bypass the spaces between sentences.
                strReader.Read();
                strReader.Read();
            }
            else
            {
                strWriter.Write(convertedCharacter);
            }
        }
        Console.WriteLine("\nOriginal text:\n\n{0}",
            strWriter.ToString());
    }
}
Option Explicit
Option Strict

Imports System.IO

Public Class StrReader

    Shared Sub Main()
    
        Dim textReaderText As String = "TextReader is the " & _
            "abstract base class of StreamReader and " & _
            "StringReader, which read characters from streams " & _
            "and strings, respectively." & _
            vbCrLf & vbCrLf & _
            "Create an instance of TextReader to open a text " & _
            "file for reading a specified range of characters, " & _
            "or to create a reader based on an existing stream." & _
            vbCrLf & vbCrLf & _
            "You can also use an instance of TextReader to read " & _
            "text from a custom backing store using the same " & _
            "APIs you would use for a string or a stream." & _
            vbCrLf & vbCrLf

        Console.WriteLine("Original text:" & vbCrLf & vbCrLf & _
            textReaderText)

        ' From textReaderText, create a continuous paragraph 
        ' with two spaces between each sentence.
        Dim aLine, aParagraph As String
        Dim strReader As New StringReader(textReaderText)
        While True
            aLine = strReader.ReadLine()
            If aLine Is Nothing Then
                aParagraph = aParagraph & vbCrLf
                Exit While
            Else
                aParagraph = aParagraph & aLine & " "
            End If
        End While
        Console.WriteLine("Modified text:" & vbCrLf & vbCrLf & _ 
            aParagraph)
    
        ' Re-create textReaderText from aParagraph.
        Dim intCharacter As Integer 
        Dim convertedCharacter As Char 
        Dim strWriter As New StringWriter()
        strReader = New StringReader(aParagraph)
        While True
            intCharacter = strReader.Read()

            ' Check for the end of the string 
            ' before converting to a character.
            If intCharacter = -1 Then
                Exit While
            End If

            convertedCharacter = Convert.ToChar(intCharacter)
            If convertedCharacter = "."C Then
                strWriter.Write("." & vbCrLf & vbCrLf)

                ' Bypass the spaces between sentences.
                strReader.Read()
                strReader.Read()
            Else
                strWriter.Write(convertedCharacter)
            End If
        End While
        Console.WriteLine(vbCrLf & "Original text:" & vbCrLf & _ 
            vbCrLf & strWriter.ToString())

    End Sub
End Class

備註

StringWriter 可讓您以同步或非同步方式寫入字串。 您可以使用 或 WriteAsync(Char) 方法一次撰寫字元,一次 Write(Char) 使用 Write(String)WriteAsync(String) 方法撰寫字串。 此外,您也可以使用其中 WriteLineAsync 一種方法,以非同步方式撰寫字元、字元陣列或字串,後面接著行結束字元。

注意

此類型會實作 IDisposable 介面,但實際上沒有任何要處置的資源。 這表示其處置方式不一定要直接呼叫 Dispose() 或使用語言建構,例如 using (在 C# 中) 或 Using (在 Visual Basic 中)。

下表列出其他一般或相關 I/O 工作的範例。

作法... 請參閱這個主題中的範例…
建立文字檔 作法:將文字寫入檔案
寫入文字檔。 作法:將文字寫入檔案
從文字檔讀取。 作法:讀取檔案中的文字
將文字附加至檔案。 作法:開啟並附加至記錄檔

File.AppendText

FileInfo.AppendText
取得檔案的大小。 FileInfo.Length
取得檔案的屬性。 File.GetAttributes
設定檔案的屬性。 File.SetAttributes
判斷檔案是否存在。 File.Exists
從二進位檔案讀取。 作法:讀取和寫入新建立的資料檔案
寫入二進位檔案。 作法:讀取和寫入新建立的資料檔案

建構函式

StringWriter()

初始化 StringWriter 類別的新執行個體。

StringWriter(IFormatProvider)

使用指定的格式控制項,初始化 StringWriter 類別的新執行個體。

StringWriter(StringBuilder)

初始化 StringWriter 類別的新執行個體,這個執行個體可將資源寫入指定的 StringBuilder

StringWriter(StringBuilder, IFormatProvider)

初始化 StringWriter 類別的新執行個體,這會寫入指定的 StringBuilder,且具有指定的格式提供者。

欄位

CoreNewLine

儲存這個 TextWriter 所使用的新行字元。

(繼承來源 TextWriter)

屬性

Encoding

取得寫入輸出的 Encoding

FormatProvider

取得控制格式設定的物件。

(繼承來源 TextWriter)
NewLine

取得或設定目前 TextWriter 所使用的行結束字元字串。

(繼承來源 TextWriter)

方法

Close()

關閉目前的 StringWriter 和基礎資料流。

Close()

關閉目前寫入器並釋放任何與寫入器相關聯的系統資源。

(繼承來源 TextWriter)
CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。

(繼承來源 MarshalByRefObject)
Dispose()

釋放由 TextWriter 物件使用的所有資源。

(繼承來源 TextWriter)
Dispose(Boolean)

釋放 StringWriter 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。

DisposeAsync()

以非同步方式釋放由 TextWriter 物件使用的所有資源。

(繼承來源 TextWriter)
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
Flush()

清除目前寫入器的所有緩衝區,並造成任何緩衝資料都寫入基礎裝置。

(繼承來源 TextWriter)
FlushAsync()

以非同步的方式清除目前寫入器的所有緩衝區,並造成任何緩衝資料都寫入基礎裝置。

FlushAsync()

以非同步的方式清除目前寫入器的所有緩衝區,並造成任何緩衝資料都寫入基礎裝置。

(繼承來源 TextWriter)
FlushAsync(CancellationToken)

以非同步的方式清除目前寫入器的所有緩衝區,並造成任何緩衝資料都寫入基礎裝置。

(繼承來源 TextWriter)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetLifetimeService()
已淘汰.

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。

(繼承來源 MarshalByRefObject)
GetStringBuilder()

傳回基礎 StringBuilder

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
InitializeLifetimeService()
已淘汰.

取得存留期服務物件,以控制這個執行個體的存留期原則。

(繼承來源 MarshalByRefObject)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。

(繼承來源 MarshalByRefObject)
ToString()

傳回字串,其中包含至今寫入目前 StringWriter 的字元。

Write(Boolean)

Boolean 值的文字表示寫入文字資料流。

(繼承來源 TextWriter)
Write(Char)

將一個字元寫入字串。

Write(Char[])

將字元陣列寫入文字資料流。

(繼承來源 TextWriter)
Write(Char[], Int32, Int32)

將字元子陣列寫入此字串。

Write(Decimal)

將十進位值的文字表示寫入文字資料流。

(繼承來源 TextWriter)
Write(Double)

將 8 位元組浮點數值的文字表示寫入文字資料流。

(繼承來源 TextWriter)
Write(Int32)

將 4 位元組帶正負號的整數文字表示寫入文字資料流。

(繼承來源 TextWriter)
Write(Int64)

將 8 位元組帶正負號的整數文字表示寫入文字資料流。

(繼承來源 TextWriter)
Write(Object)

呼叫該物件的 ToString 方法,將物件的文字表示寫入文字資料流。

(繼承來源 TextWriter)
Write(ReadOnlySpan<Char>)

將字元範圍的字串表示寫入目前字串。

Write(ReadOnlySpan<Char>)

將字元範圍寫入文字資料流。

(繼承來源 TextWriter)
Write(Single)

將 4 位元組浮點數值的文字表示寫入文字資料流。

(繼承來源 TextWriter)
Write(String)

將字串寫入目前的字串。

Write(String, Object)

使用與 Format(String, Object) 方法相同的語意,將格式化字串寫入文字資料流。

(繼承來源 TextWriter)
Write(String, Object, Object)

使用與 Format(String, Object, Object) 方法相同的語意,將格式化字串寫入文字資料流。

(繼承來源 TextWriter)
Write(String, Object, Object, Object)

使用與 Format(String, Object, Object, Object) 方法相同的語意,將格式化字串寫入文字資料流。

(繼承來源 TextWriter)
Write(String, Object[])

使用與 Format(String, Object[]) 方法相同的語意,將格式化字串寫入文字資料流。

(繼承來源 TextWriter)
Write(StringBuilder)

將字串產生器的字串表示寫入目前字串。

Write(StringBuilder)

將字串產生器寫入文字資料流。

(繼承來源 TextWriter)
Write(UInt32)

將 4 位元組不帶正負號的整數文字表示寫入文字資料流。

(繼承來源 TextWriter)
Write(UInt64)

將 8 位元組帶不正負號的整數文字表示寫入文字資料流。

(繼承來源 TextWriter)
WriteAsync(Char)

以非同步方式將字元寫入字串。

WriteAsync(Char)

以非同步方式將字元寫入文字資料流。

(繼承來源 TextWriter)
WriteAsync(Char[])

以非同步方式將字元陣列寫入文字資料流。

(繼承來源 TextWriter)
WriteAsync(Char[], Int32, Int32)

以非同步方式將字元的子陣列寫入字串。

WriteAsync(Char[], Int32, Int32)

以非同步方式將字元的子陣列寫入文字資料流。

(繼承來源 TextWriter)
WriteAsync(ReadOnlyMemory<Char>, CancellationToken)

以非同步方式將字元的記憶體區域寫入字串。

WriteAsync(ReadOnlyMemory<Char>, CancellationToken)

以非同步方式將字元記憶體區域寫入文字資料流。

(繼承來源 TextWriter)
WriteAsync(String)

將字串以非同步方式寫入目前的字串。

WriteAsync(String)

以非同步方式將字串寫入至文字資料流。

(繼承來源 TextWriter)
WriteAsync(StringBuilder, CancellationToken)

以非同步方式將字串產生器的文字表示寫入字串。

WriteAsync(StringBuilder, CancellationToken)

以非同步方式將字串產生器寫入文字資料流。

(繼承來源 TextWriter)
WriteLine()

將行結束字元寫入文字資料流。

(繼承來源 TextWriter)
WriteLine(Boolean)

Boolean 值的文字表示寫入文字資料流,後接行結束字元。

(繼承來源 TextWriter)
WriteLine(Char)

將字元寫入文字資料流,後接行結束字元。

(繼承來源 TextWriter)
WriteLine(Char[])

將字元的陣列寫入文字資料流,後接行結束字元。

(繼承來源 TextWriter)
WriteLine(Char[], Int32, Int32)

將字元的子陣列寫入文字資料流,後接行結束字元。

(繼承來源 TextWriter)
WriteLine(Decimal)

將十進位值的文字表示寫入文字資料流,後接行結束字元。

(繼承來源 TextWriter)
WriteLine(Double)

將 8 位元組浮點數值的文字表示寫入文字資料流,後接行結束字元。

(繼承來源 TextWriter)
WriteLine(Int32)

將 4 位元組帶正負號的整數文字表示寫入文字資料流,後接行結束字元。

(繼承來源 TextWriter)
WriteLine(Int64)

將 8 位元組帶正負號的整數文字表示寫入文字資料流,後接行結束字元。

(繼承來源 TextWriter)
WriteLine(Object)

藉由呼叫該物件上的 ToString 方法,後接行結束字元,將物件的文字表示寫入文字資料流。

(繼承來源 TextWriter)
WriteLine(ReadOnlySpan<Char>)

將字元範圍的文字表示寫入字串,後接行結束字元。

WriteLine(ReadOnlySpan<Char>)

將字元範圍的文字表示寫入文字資料流,後接行結束字元。

(繼承來源 TextWriter)
WriteLine(Single)

將 4 位元組浮點數值的文字表示寫入文字資料流,後接行結束字元。

(繼承來源 TextWriter)
WriteLine(String)

將字串寫入文字資料流,後接行結束字元。

(繼承來源 TextWriter)
WriteLine(String, Object)

使用與 Format(String, Object) 方法相同的語意,將格式化字串和新行寫入文字資料流。

(繼承來源 TextWriter)
WriteLine(String, Object, Object)

使用與 Format(String, Object, Object) 方法相同的語意,將格式化字串和新行寫入文字資料流。

(繼承來源 TextWriter)
WriteLine(String, Object, Object, Object)

使用與 Format(String, Object) 相同的語意,寫出文字資料流中的格式化字串和新行。

(繼承來源 TextWriter)
WriteLine(String, Object[])

使用與 Format(String, Object) 相同的語意,寫出文字資料流中的格式化字串和新行。

(繼承來源 TextWriter)
WriteLine(StringBuilder)

將字串產生器的文字表示寫入字串,後接行結束字元。

WriteLine(StringBuilder)

將字串產生器的文字表示寫入文字資料流,後接行結束字元。

(繼承來源 TextWriter)
WriteLine(UInt32)

將 4 位元組不帶正負號的整數文字表示寫入文字資料流,後接行結束字元。

(繼承來源 TextWriter)
WriteLine(UInt64)

將 8 位元組不帶正負號的整數文字表示寫入文字資料流,後接行結束字元。

(繼承來源 TextWriter)
WriteLineAsync()

以非同步方式將行結束字元寫入文字資料流。

(繼承來源 TextWriter)
WriteLineAsync(Char)

以非同步方式將字元寫入字串,後接行結束字元。

WriteLineAsync(Char)

以非同步方式將字元寫入文字資料流,後接行結束字元。

(繼承來源 TextWriter)
WriteLineAsync(Char[])

以非同步方式將字元的陣列寫入文字資料流,後接行結束字元。

(繼承來源 TextWriter)
WriteLineAsync(Char[], Int32, Int32)

以非同步方式將字元的子陣列寫入字串,後接行結束字元。

WriteLineAsync(Char[], Int32, Int32)

以非同步方式將字元的子陣列寫入文字資料流,後接行結束字元。

(繼承來源 TextWriter)
WriteLineAsync(ReadOnlyMemory<Char>, CancellationToken)

以非同步方式將字元的記憶體區域字串表示寫入目前字串,後接行結束字元。

WriteLineAsync(ReadOnlyMemory<Char>, CancellationToken)

以非同步方式將字元記憶體區域的文字表示寫入文字資料流,後接行結束字元。

(繼承來源 TextWriter)
WriteLineAsync(String)

以非同步方式將字串寫入目前字串,後接行結束字元。

WriteLineAsync(String)

以非同步方式將字串寫入文字資料流,後接行結束字元。

(繼承來源 TextWriter)
WriteLineAsync(StringBuilder, CancellationToken)

以非同步方式將字串產生器的字串表示寫入目前字串,後接行結束字元。

WriteLineAsync(StringBuilder, CancellationToken)

以非同步方式將字串產生器的文字表示寫入文字資料流,後接行結束字元。

(繼承來源 TextWriter)

明確介面實作

IDisposable.Dispose()

如需這個成員的說明,請參閱 Dispose()

(繼承來源 TextWriter)

擴充方法

ConfigureAwait(IAsyncDisposable, Boolean)

設定如何執行從非同步可處置項目傳回的工作 await。

適用於

另請參閱