首頁  >  文章  >  後端開發  >  C# 字串編寫器

C# 字串編寫器

WBOY
WBOY原創
2024-09-03 15:23:18326瀏覽

C#中的StringWriter類別派生自TextWriter子類,可以使用StringWriter類別操作字串,並且該StringWriter類別用於寫入屬於System.StringBuilder類別。使用此StringBuilder 類別可以有效地建構文字命名空間和字串,因為字串在C# 中是不可變的,並且StringWriter 提供了Write 和WriteLine 方法,以便能夠寫入StringBuilder 的對象,並且可以同步完成寫入字符串異步方式,StringBuilder類別儲存StringWriter類別寫入的資訊。

文法:

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

工作和建構子

為了理解C#中StringWriter類別的工作原理,我們需要了解StringWriter類別的建構子、StringWriter類別的屬性和StringWriter類別的方法。

  • StringWriter():使用 StringWriter() 方法初始化 StringWriter 類別的新實例。
  • StringWriter(IFormatProvider):使用 (StringWriter(IFormatProvider) 方法初始化 StringWriter 類別的新實例,並將格式控制指定為參數。
  • StringWriter(StringBuilder):使用 StringWriter(IFormatProvider) 方法初始化 StringWriter 類別的新實例,並將格式控制指定為參數。
  • StringWriter(StringBuilder,?IFormatProvider): StringWriter 類別的新實例被初始化以寫入指定為第一個參數的 StringBuilder,並將格式提供者指定為第二個參數。

C# StringWriter 類別的屬性

StringWriter類別有幾個屬性。它們的解釋如下:

  • 編碼:對 C# 中 StringWriter 類別的屬性進行編碼用於取得我們寫入輸出的編碼。
  • FormatProvider:C#中StringWriter類別的FormatProvider屬性用於取得進行格式控制的物件。
  • NewLine:C#中StringWriter類別的NewLine屬性用於取得或設定行結束符字串,該字串行結束符由目前TextWriter使用。

C# 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# StringWriter 的實作範例

以下是 C# StringReader 類別的範例:

範例#1

代碼:

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();
}
}
}

輸出:

C# 字串編寫器

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.

Example #2

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:

C# 字串編寫器

Conclusion

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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:C# 字串讀取器下一篇:C# 字串讀取器