📌  相关文章
📜  C#StringWriter

📅  最后修改于: 2020-10-31 04:16:14             🧑  作者: Mango

C#StringWriter类别

此类用于写入和处理字符串数据而不是文件。它是TextWriter类的派生类。由StringWriter类编写的字符串数据存储在StringBuilder中。

此类的目的是操纵字符串并将结果保存到StringBuilder中。

StringWriter类签名

[SerializableAttribute]
[ComVisibleAttribute(true)]
public class StringWriter : TextWriter

C#StringWriter构造函数

Constructors Description
StringWriter() It is used to initialize a new instance of the StringWriter class.
StringWriter(IFormatProvider) It is used to initialize a new instance of the StringWriter class with the specified format control.
StringWriter(StringBuilder) It is used to initialize a new instance of the StringWriter class that writes to the specified StringBuilder.
StringWriter(StringBuilder,?IFormatProvider) It is used to initialize a new instance of the StringWriter class that writes to the specified StringBuilder and has the specified format provider.

C#StringWriter属性

Property Description
Encoding It is used to get the Encoding in which the output is written.
FormatProvider It is used to get an object that controls formatting.
NewLine It is used to get or set the line terminator string used by the current TextWriter.

C#StringWriter方法

Methods Description
Close() It is used to close the current StringWriter and the underlying stream.
Dispose() It is used to release all resources used by the TextWriter object.
Equals(Object) It is used to determine whether the specified object is equal to the current object or not.
Finalize() It allows an object to try to free resources and perform other cleanup operations.
GetHashCode() It is used to serve as the default hash function.
GetStringBuilder() It returns the underlying StringBuilder.
ToString() It returns a string containing the characters written to the current StringWriter.
WriteAsync(String) It is used to write a string to the current string asynchronously.
Write(Boolean) It is used to write the text representation of a Boolean value to the string.
Write(String) It is used to write a string to the current string.
WriteLine(String) It is used to write a string followed by a line terminator to the string or stream.
WriteLineAsync(String) Writes a string followed by a line terminator asynchronously to the current string.(Overrides TextWriter.WriteLineAsync(String).)

C#StringWriter示例

在下面的程序中,我们使用StringWriter类将字符串信息写入StringBuilder类。 StringReader类用于将书面信息读取到StringBuilder。

using System;
using System.IO;
using System.Text;
namespace CSharpProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            string text = "Hello, Welcome to the javatpoint \n" +
                "It is nice site. \n" +
                "It provides technical tutorials";
            // Creating StringBuilder instance
            StringBuilder sb = new StringBuilder();
            // Passing StringBuilder instance into StringWriter
            StringWriter writer = new StringWriter(sb);
            // Writing data using StringWriter
            writer.WriteLine(text);
            writer.Flush();
            // Closing writer connection
            writer.Close();
            // Creating StringReader instance and passing StringBuilder
            StringReader reader = new StringReader(sb.ToString());
            // Reading data
            while (reader.Peek() > -1)
            {
                Console.WriteLine(reader.ReadLine());
            }
        }
    }
}

输出:

Hello, Welcome to the javatpoint
It is nice site.
It provides technical tutorials