ホームページ  >  記事  >  バックエンド開発  >  C# 文字列ライター

C# 文字列ライター

WBOY
WBOYオリジナル
2024-09-03 15:23:18326ブラウズ

C# の StringWriter クラスは TextWriter サブクラスから派生し、文字列は StringWriter クラスを使用して操作できます。この StringWriter クラスは、System に属する StringBuilder クラスに書き込むために使用されます。 C# では文字列は不変であり、StringWriter によって Write および WriteLine メソッドが提供され、StringBuilder のオブジェクトに書き込めるようになり、文字列への書き込みは同期で実行できるため、この StringBuilder クラスを使用して Text 名前空間と文字列を効率的に構築できます。非同期方式であり、StringWriterクラスで書き込まれた情報をStringBuilderクラスに格納します。

構文:

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

作業と コンストラクター

C# の StringWriter クラスの動作を理解するには、StringWriter クラスのコンストラクター、StringWriter クラスのプロパティ、および StringWriter クラスのメソッドを理解する必要があります。

  • StringWriter(): StringWriter クラスの新しいインスタンスは、StringWriter() メソッドを使用して初期化されます。
  • StringWriter(IFormatProvider): StringWriter クラスの新しいインスタンスは、パラメーターとして指定された形式制御を持つ (StringWriter(IFormatProvider) メソッドを使用して初期化されます。
  • StringWriter(StringBuilder): StringWriter クラスの新しいインスタンスは、パラメーターとして指定された形式制御を持つ StringWriter(IFormatProvider) メソッドを使用して初期化されます。
  • StringWriter(StringBuilder,?IFormatProvider): StringWriter クラスの新しいインスタンスは、最初のパラメーターとして指定された StringBuilder に書き込むように初期化され、2 番目のパラメーターとして指定された形式プロバイダーを持ちます。

C# StringWriter クラスのプロパティ

StringWriter クラスにはいくつかのプロパティがあります。それらは次のように説明されます:

  • エンコーディング: C# の StringWriter クラスのプロパティのエンコーディングは、出力を書き込むエンコーディングを取得するために使用されます。
  • FormatProvider: C# の StringWriter クラスの FormatProvider プロパティは、形式の制御を行うオブジェクトを取得するために使用されます。
  • NewLine: C# の StringWriter クラスの NewLine プロパティは、行終了文字列を取得または設定するために使用され、この行終了文字列は現在の TextWriter によって使用されます。
C# StringWriter クラスのメソッド

StringWriter クラスにはいくつかのメソッドがあります。それらは次のように説明されます:

1. Close(): StringWriter とストリームは Close() メソッドを使用して閉じることができます。

2. Dispose(): TextWriter のオブジェクトで使用されているすべてのリソースは、dispose() メソッドを使用して解放できます。

3. Equals(Object): Equals(Object) メソッドは、指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断するために使用されます。

4. Finalize(): オブジェクトは、それ自体が占有しているリソースを解放し、Finalize() メソッドを使用して他のクリーンアップ操作を実行できます。

5. GetHashCode(): GetHashCode() メソッドはデフォルトでハッシュ関数として使用できます。

6. GetStringBuilder(): 基になる StringBuilder は、GetStringBuilder() メソッドを使用して返されます。

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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。