StringWriter vs. StringReader in C#?

WBOY
Release: 2023-09-15 16:17:02
forward
884 people have browsed it

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);
   }
}
}
Copy after login

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);
}
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!