StringWriter 类 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
实现用于将信息写入字符串的 TextWriter。 信息存储在基础 StringBuilder中。
public ref class StringWriter : System::IO::TextWriterpublic 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.TextWritertype StringWriter = class
    inherit TextWriter[<System.Serializable>]
type StringWriter = class
    inherit TextWriter[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type StringWriter = class
    inherit TextWriterPublic 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) | 初始化写入指定 StringBuilder的 StringWriter 类的新实例。 | 
| StringWriter(StringBuilder, IFormatProvider) | 初始化 StringWriter 类的新实例,该实例写入指定的 StringBuilder 并具有指定的格式提供程序。 | 
字段
| CoreNewLine | 存储用于此  | 
属性
| Encoding | 获取在其中写入输出的 Encoding。 | 
| FormatProvider | 获取一个对象,该对象控制格式设置。(继承自 TextWriter) | 
| NewLine | 获取或设置当前  | 
方法
显式接口实现
| IDisposable.Dispose() | 有关此成员的说明,请参阅 Dispose()。(继承自 TextWriter) | 
扩展方法
| ConfigureAwait(IAsyncDisposable, Boolean) | 配置如何执行从异步可释放项返回的任务的 await。 |