Home > Article > Backend Development > StringWriter vs. StringReader in C#?
StringReader and StringWriter are derived from TextReader and TextWriter
StringWriter is used for writing to a string buffer. It implements a TextWriter for writing information to a string.
For StringWriter -
StringWriter sWriter = new StringWriter(); while(true) { myChar = strReader.Read(); if(myChar == -1) break; convertedChar = Convert.ToChar(myChar); if(convertedChar == '.') { strWriter.Write("."); sReader.Read(); sReader.Read(); }else { sWriter.Write(convertedChar); } } }
StringReader to read string -
StringBuilder sbuilder = new StringBuilder(); // append sbuilder.AppendLine("Line one characters"); sbuilder.AppendLine("Line two characters"); sbuilder.AppendLine("Line three characters"); // read string using (StringReader sReader = new StringReader(sbuilder.ToString())) { string readString = await sReader.ReadToEndAsync(); Console.WriteLine(readString); }
The above is the detailed content of StringWriter vs. StringReader in C#?. For more information, please follow other related articles on the PHP Chinese website!