C# StringReader Tutorial with Programming Examples and Codes

In this chapter you will learn:
  • What is StringReader class in C#?
  • Programming Examples and Codes.

What is StringReader Class in C#?

StringReader class implements TextReader class that reads string from string. It enables you to read a string synchronously or asynchronously. You can read a character with Read() method and read a line with ReadLine() method.

Programming Examples and Codes

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace StringReader_Class
{
    class Program
    {
        static void Main(string[] args)
        {            
            string text = @"You are reading
this article at
completecsharptutorial.com";

            using (StringReader reader = new StringReader(text))
            {
                int count = 0;
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    count++;
                    Console.WriteLine("Line {0}: {1}", count, line);
                }
            }
            Console.ReadKey();
        }
    }
}

  Output
Line 1: You are reading
Line 2: this article at
Line 3: completecsharptutorial.com
_

Explanation

In this program StringReader class reads a string from string variable and marks each line with count number and shows display on the console.

Summary

In this chapter you learned StringReader class in C# with programming examples and codes. In the next chapter you will learn about DirectoryInfo class in C#.

More Articles

 

Share your thought