StringWriter 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
實作 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
- 繼承
- 繼承
- 屬性
範例
下列程式代碼範例示範如何從一組雙空格句子建立連續段落,然後將段落轉換回原始文字。
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 可讓您以同步或異步方式寫入字串。 您可以使用 Write(Char) 或 WriteAsync(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 |
儲存用於這個 |
屬性
Encoding |
取得寫入輸出的 Encoding。 |
FormatProvider |
取得控制項格式設定的物件。 (繼承來源 TextWriter) |
NewLine |
取得或設定目前 |
方法
明確介面實作
IDisposable.Dispose() |
如需此成員的描述,請參閱 Dispose()。 (繼承來源 TextWriter) |
擴充方法
ConfigureAwait(IAsyncDisposable, Boolean) |
設定如何執行從異步可處置專案傳回的工作等候。 |