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對(duì)象的流讀取操作,從而更有效地處理字符串?dāng)?shù)據(jù)。