C# 文字列リーダー

王林
リリース: 2024-09-03 15:23:10
オリジナル
941 人が閲覧しました

C# の StringReader クラスは TextReader サブクラスから派生し、文字列は StringReader クラスを使用して操作できます。この StringReader クラスは文字列を使用して構築され、文字列の一部とデータを読み取るための Read メソッドと ReadLine メソッドが StringReader クラスによって提供されます。 StringReader クラスによって読み取られるデータは、TextWriter サブクラスから派生した StringWriter クラスによって書き込まれたデータであり、データは StringReader クラスを使用して同期または非同期で読み取ることができ、読み取り操作は存在するコンストラクターとメソッドを使用して実行されます。このクラスでは。

構文:

[SerializableAttribute]
[ComVisibleAttribute(true)]
public class StringReader : TextReader
ログイン後にコピー

C# StringReader の動作とコンストラクター

C# の StringReader クラスの動作を理解するには、StringReader クラスのコンストラクターと StringReader クラスのメソッドを理解する必要があります。

  • StringReader(string): StringReader クラスの新しいインスタンスが初期化され、指定された文字列から読み取ります。

C# StringReader クラスのメソッド

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

1. Close(): StringReader は Close() メソッドを使用して閉じることができます。

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

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

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

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

6. GetType(): 現在のインスタンスのタイプは、GetType() メソッドを使用して取得できます。

7. Peek(): 使用可能な次の文字は Peek() メソッドを使用して返すことができ、このメソッドは次に使用可能な文字を消費しません。

8. Read(): 入力文字列の次の文字は、Read() メソッドを使用して読み取ることができます。

9. ReadLine(): 現在の文字列に存在する文字の行は、ReadLine() メソッドを使用して読み取ることができます。

10. ReadLineAsync(): 現在の文字列に存在する文字の行は、ReadLineAsync() メソッドを使用して非同期的に読み取ることができます。

11. ReadToEnd(): ReadToEnd() メソッドを使用すると、文字列の現在位置から文字列の終了位置まで、文字列のすべての文字を読み取ることができます。

12. ReadToEndAsync(): ReadToEndAsync() メソッドを使用すると、文字列の現在位置から文字列の終了位置まで、文字列のすべての文字を非同期で読み取ることができます。

13. ToString(): ToString() メソッドを使用して、現在のオブジェクトを表す文字列が返されます。

C# StringReader の実装例

以下は C# StringReader クラスの例です:

例 #1

コード:

using System;
using System.IO;
namespace Program
{
class Check
{
//calling the main method
static void Main(string[] args)
{
//creating an instance of stringwriter method
StringWriter strng = new StringWriter();
//writing data using stringwriter instance
strng.WriteLine("Hello, welcome to StringReader class");
//closing the instance of stringwriter
strng.Close();
// Creating an instance of stringreader to which the stringwriter instance is passed
StringReader read = new StringReader(strng.ToString());
// data written using stringwriter is read using stringreader
while (read.Peek() > -1)
{
Console.WriteLine(read.ReadLine());
}
}
}
}
ログイン後にコピー

出力:

C# 文字列リーダー

上記のプログラムでは、program という名前空間が宣言されています。次に、main メソッドが呼び出されます。次に、StringWriter メソッドのインスタンスが作成されます。作成した StringWriter クラスのインスタンスを使用して、データが書き込まれます。ここで書かれるデータは「こんにちは、StringReader クラスへようこそ」です。次に、Close() メソッドを使用して StringWriter クラスのインスタンスを閉じます。次に、StringReader クラスのインスタンスが作成され、StringWriter クラスのインスタンスがパラメータとして渡されます。 StringWriter クラスのインスタンスを使用して書き込まれたデータは、StringReader クラスのインスタンスを使用して読み取られ、同じものが出力に表示されます。

例 #2

StringReader クラスの ReadToEnd() メソッドの使用法を示す C# プログラム。

コード:

using System;
using System.IO;
using System.Text;
namespace Program
{
class Check
{
//calling the main method
static void Main(string[] args)
{
//Creating an instance of StringBuilder class
var readall = new StringBuilder();
//appending all the single statements into one through the instance of StringBuilder class
readall.AppendLine("Welcome to StringReader class.");
readall.AppendLine("Lets understand ReadToEnd() method.");
readall.AppendLine("This method displays all the statements right from the beginning.");
//creating an instance of StringReader class
var read = new StringReader(readall.ToString());
//calling the ReadToEnd() method using the instance of StringReader class
string tt = read.ReadToEnd();
Console.WriteLine(tt);
}
}
}
ログイン後にコピー

出力:

C# 文字列リーダー

例 #3

StringReader クラスの Read() メソッドの使用法を示す C# プログラム。

コード:

using System;
using System.IO;
namespace Program
{
class Check
{
//Calling the main method
static void Main(string[] args)
{
//A string variable holding a string of characters is defined
var tt = "Welcome to StringReader class.";
//an instance of the stringreader class is created
var read = new StringReader(tt);
//a counter is declared and initialized to zero
int count1 = 0;
//the occurrence of the character to be identified from the statement is assigned to a character variable
char ch = 'e';
//an integer variable is declared
int x;
//Read() method is called and checked if the control has not reached the end of the string or if the string do not exist
while ((x = read.Read()) != -1)
{
char ct = (char) x;
//if the character whose occurrence must be counted is same as the character in the statement while traversing through the statement, the counter is incremented by one
if (ct.Equals(ch))
{
count1++;
}
}
//finally the number of occurrences of the specified character is displayed
Console.WriteLine($"The number of '{ch}' characters in the given statement is {count1}");
}
}
}
ログイン後にコピー

出力:

C# 文字列リーダー

結論

このチュートリアルでは、定義、StringReader クラスのコンストラクター、StringReader クラスのメソッドを通じて C# の StringReader クラスの概念を理解し、プログラミング例と StringReader クラスのメソッドを示す出力を通じて StringReader クラスの動作を理解します。

以上がC# 文字列リーダーの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!