首页  >  文章  >  后端开发  >  C# 字符串读取器

C# 字符串读取器

王林
王林原创
2024-09-03 15:23:10945浏览

C#中的StringReader类派生自TextReader子类,可以使用StringReader类来操作字符串,并且这个StringReader类是使用字符串构建的,并且StringReader类提供了Read和ReadLine方法来读取字符串的部分和数据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(): 可以使用 Close() 方法关闭 StringReader。

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# 字符串读取器

在上面的程序中,声明了一个名为程序的命名空间。然后调用main方法。然后创建 StringWriter 方法的实例。使用刚刚创建的 StringWriter 类的实例写入数据。这里写入的数据是“你好,欢迎来到StringReader类”。然后使用 Close() 方法关闭 StringWriter 类的实例。然后创建 StringReader 类的实例,并将 StringWriter 类的实例作为参数传递给该实例。使用 StringReader 类的实例读取使用 StringWriter 类的实例写入的数据,并将其显示在输出中。

示例#2

C# 程序演示 StringReader 类的 ReadToEnd() 方法的用法。

代码:

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

C# 程序演示 StringReader 类的 Read() 方法的用法。

代码:

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中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:TextReader in C#下一篇:C# StringWriter