C#中的StringWriter類別派生自TextWriter子類,可以使用StringWriter類別操作字串,並且該StringWriter類別用於寫入屬於System.StringBuilder類別。使用此StringBuilder 類別可以有效地建構文字命名空間和字串,因為字串在C# 中是不可變的,並且StringWriter 提供了Write 和WriteLine 方法,以便能夠寫入StringBuilder 的對象,並且可以同步完成寫入字符串異步方式,StringBuilder類別儲存StringWriter類別寫入的資訊。
文法:
[SerializableAttribute] [ComVisibleAttribute(true)] public class StringWriter : TextWriter
為了理解C#中StringWriter類別的工作原理,我們需要了解StringWriter類別的建構子、StringWriter類別的屬性和StringWriter類別的方法。
StringWriter類別有幾個屬性。它們的解釋如下:
StringWriter類別有多種方法。它們的解釋如下:
1。 Close(): 可以使用 Close() 方法關閉 StringWriter 和流。
2。 Dispose(): TextWriter物件使用的所有資源都可以使用dispose()方法釋放。
3。 Equals(Object):Equals(Object)方法用來判斷指定物件是否等於目前物件。
4。 Finalize():物件可以使用 Finalize() 方法釋放自身佔用的資源並執行其他清理操作。
5。 GetHashCode():GetHashCode()方法預設可用作雜湊函數。
6。 GetStringBuilder(): 使用 GetStringBuilder() 方法傳回底層 StringBuilder。
7。 ToString():使用 ToString() 方法將由字元組成的字串傳回 StringWriter。
8。 WriteAsync(String): 使用 WriteAsync(String) 方法將字串非同步寫入指定為參數的字串。
9。 Write(Boolean):指定為參數的布林值以文字形式表示,並使用 Write(Boolean) 方法寫入字串。
10。 Write(String):使用 Write(String) 方法將字串寫入指定為參數的目前字串。
11。 WriteLine(String):使用 WriteLine(String) 方法將後接行終止符的字串寫入指定為參數的目前字串。
12。 WriteLineAsync(): 使用 WriteLineAsync(String) 方法將後跟行終止符的字串非同步寫入指定為參數的目前字串。
以下是 C# StringReader 類別的範例:
代碼:
using System using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Program { class Check { //calling the main method static void Main(string[] args) { //define a string to hold the path of the file containing data String str = @"D:\Ex.txt"; //create an instance of the stream writer class and pass the string containing the path of the file to appendtext() method. using (StreamWriter sw = File.AppendText(str)) { //using the instance of the stringwriter class, write the data to the file sw.WriteLine("Welcome to StringWriter class"); sw.Close(); //using the string containing the path of the file, the contents of the file are read Console.WriteLine(File.ReadAllText(str)); } Console.ReadKey(); } } }
輸出:
In the above program, a namespace called the program is declared. Then the main method is called. Then a string is declared which holds the path of the file in which the data will be written. Then an instance of the StringWriter method is created which is assigned to the appendtext() method to which the string containing the path of the file is passed as a parameter. Using the instance of the StringWriter class that was just created, data is written to the file Ex.txt. Here the data written is “Welcome to StringWriter class.” Then the instance of the StringWriter class is closed using the Close() method. Then using the string containing the path of the file, the contents of the file are read and the same is displayed in the output.
C# program to demonstrate usage of WriteLine() method of StringWriter class.
Code :
using System; using System.IO; using System.Text; namespace Program { class Check { //Main method is called static void Main(string[] args) { //define a string to hold the data to be displayed string str = "Hello, Welcome to the StringWriter class \n" + "This tutorial is for learning \n" + "Learning is fun"; // An instance of the string builder class is created StringBuilder build = new StringBuilder(); // an instance of the stringwriter class is created and the instance of the stringbuilder class is passed as a parameter to stringwriter class StringWriter write = new StringWriter(build); // data is written using string writer writeline() method write.WriteLine(str); write.Flush(); // the instance of the stringwriter is closed write.Close(); // an instance of stringreader class is created to which the instance of stringbuilder class is passed as a parameter StringReader read = new StringReader(build.ToString()); while (read.Peek() > -1) { Console.WriteLine(read.ReadLine()); } } } }
Output:
In this tutorial, we understand the concept of StringWriter class in C# through definition, constructors of StringWriter class, properties of StringWriter class, and methods of StringWriter class, working of StringWriter class through programming examples and their outputs demonstrating the methods of StringWriter class.
以上是C# 字串編寫器的詳細內容。更多資訊請關注PHP中文網其他相關文章!