Home  >  Article  >  Backend Development  >  StringWriter vs. StringReader in C#?

StringWriter vs. StringReader in C#?

WBOY
WBOYforward
2023-09-15 16:17:02915browse

C# 中的 StringWriter 与 StringReader?

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 -

Example

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 -

Example

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete