StringReader类用于读取StringWriter类写入的数据。它是TextReader类的子类。它使无涯教程能够同步或异步读取字符串。它提供用于执行读取操作的构造函数和方法。
[SerializableAttribute] [ComVisibleAttribute(true)] public class StringReader : TextReader
StringReader具有以下构造函数。
链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/c-sharp-stringreader.html
来源:LearnFk无涯教程网
Constructors | Description |
---|---|
StringReader(String) | 初始化从指定字符串读取的 StringReader 类的新实例。 |
以下是StringReader类的方法。
Method | Description |
---|---|
Close() | 它用于关闭 StringReader。 |
Dispose() | 它用于释放 TextReader 对象使用的所有资源。 |
Equals(Object) | 它确定指定对象是否等于当前对象。 |
Finalize() | 它允许对象尝试释放资源并执行其他清理操作。 |
GetHashCode() | 它用作默认的哈希函数。 |
GetType() | 它用于获取当前实例的类型。 |
Peek() | 它用于返回下一个可用字符,但不使用它。 |
Read() | 它用于从输入字符串中读取下一个字符。 |
ReadLine() | 它用于从当前字符串中读取一行字符。 |
ReadLineAsync() | 它用于从当前字符串异步读取一行字符。 |
ReadToEnd() | 它用于读取从当前位置到字符串末尾的所有字符。 |
ReadToEndAsync() | 它用于异步读取从当前位置到字符串末尾的所有字符。 |
ToString() | 它用于返回表示当前对象的字符串。 |
在下面的示例中,StringWriter类用于写入字符串信息,StringReader类用于读取由StringWriter类写入的字符串。
using System; using System.IO; namespace CSharpProgram { class Program { static void Main(string[] args) { StringWriter str = new StringWriter(); str.WriteLine("Hello, this message is read by StringReader class"); str.Close(); // 创建 StringReader 实例并传递 StringWriter StringReader reader = new StringReader(str.ToString()); // 读取数据 while (reader.Peek() > -1) { Console.WriteLine(reader.ReadLine()); } } } }
输出:
Hello, this message is read by StringReader class
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)