StringReader类在C#中用于从字符串读取字符流。以下是一些流控制技巧,可以帮助您有效地使用StringReader类:
StringReader reader = new StringReader("Hello World");
int ch;
while ((ch = reader.Read()) != -1)
{
Console.Write((char)ch);
}
StringReader reader = new StringReader("Hello World");
int ch = reader.Peek();
Console.WriteLine((char)ch); // Output: H
StringReader reader = new StringReader("Hello\nWorld");
string line;
while ((line = reader.ReadLine()) != null)
{
Console.WriteLine(line);
}
StringReader reader = new StringReader("Hello World");
string remaining = reader.ReadToEnd();
Console.WriteLine(remaining); // Output: World
StringReader reader = new StringReader("Hello World");
reader.Close();
这些技巧可以帮助您更好地控制StringReader对象的流读取操作,从而更有效地处理字符串数据。