Updated April 4, 2023
Introduction to C# StringReader
The StringReader class in C# is derived from TextReader subclass and strings can be manipulated using StringReader class and this StringReader class is built using a string and Read and ReadLine methods are provided by StringReader class to read the parts of the string and the data read by the StringReader class is the data written by the StringWriter class which is derived from TextWriter subclass and the data can be read in a synchronous manner or in an asynchronous manner using StringReader class and the read operations are performed by using the constructors and methods present in this class.
Syntax:
[SerializableAttribute]
[ComVisibleAttribute(true)]
public class StringReader : TextReader
Working & Constructors of C# StringReader
In order to understand the working of the StringReader class in C#, we need to understand the constructors of the StringReader class and methods of StringReader class.
- StringReader(string): A new instance of the StringReader class is initialized and it reads from the specified string.
Methods of C# StringReader Class
There are several methods of StringReader class. They are explained as follows:
1. Close(): The StringReader can be closed using Close() method.
2. Dispose(): All the resources used by the object of TextReader can be released using the dispose() method.
3. Equals(Object): Equals(Object) method is used to determine if the specified object is equal to the current object or not.
4. Finalize(): An object can free the resources occupied by itself and perform other operations of cleanup using Finalize() method.
5. GetHashCode(): GetHashCode() method can be used as the hash function by default.
6. GetType(): The type of the current instance can be obtained using GetType() method.
7. Peek(): The next character that is available can be returned using Peek() method and this method does not consume the next available character.
8. Read(): The next character from the input string can be read using Read() method.
9. ReadLine(): A line of characters present in the current string can be read using the ReadLine() method.
10. ReadLineAsync(): A line of characters present in the current string can be read asynchronously using ReadLineAsync() method.
11. ReadToEnd(): All the characters of the string can be read from the current position of the string till the end position of the string using ReadToEnd() method.
12. ReadToEndAsync(): All the characters of the string can be read asynchronously from the current position of the string till the end position of the string using ReadToEndAsync() method.
13. ToString(): A string representing the current object is returned using ToString() method.
Examples to Implement of C# StringReader
Below are the examples of C# StringReader class:
Example #1
Code :
using System;
using System.IO;
namespace Program
{
class Check
{
//calling the main method
static void Main(string[] args)
{
//creating an instance of stringwriter method
StringWriter strng = new StringWriter();
//writing data using stringwriter instance
strng.WriteLine("Hello, welcome to StringReader class");
//closing the instance of stringwriter
strng.Close();
// Creating an instance of stringreader to which the stringwriter instance is passed
StringReader read = new StringReader(strng.ToString());
// data written using stringwriter is read using stringreader
while (read.Peek() > -1)
{
Console.WriteLine(read.ReadLine());
}
}
}
}
Output:
In the above program, a namespace called the program is declared. Then the main method is called. Then an instance of the StringWriter method is created. Using the instance of the StringWriter class that was just created, data is written. Here the data written is “Hello, welcome to StringReader class.” Then the instance of the StringWriter class is closed using the Close() method. Then an instance of the StringReader class is created to which the instance of the StringWriter class is passed as a parameter. The data that was written using the instance of the StringWriter class is read using the instance of the StringReader class and the same is displayed in the output.
Example #2
C# program to demonstrate usage of ReadToEnd() method of StringReader class.
Code:
using System;
using System.IO;
using System.Text;
namespace Program
{
class Check
{
//calling the main method
static void Main(string[] args)
{
//Creating an instance of StringBuilder class
var readall = new StringBuilder();
//appending all the single statements into one through the instance of StringBuilder class
readall.AppendLine("Welcome to StringReader class.");
readall.AppendLine("Lets understand ReadToEnd() method.");
readall.AppendLine("This method displays all the statements right from the beginning.");
//creating an instance of StringReader class
var read = new StringReader(readall.ToString());
//calling the ReadToEnd() method using the instance of StringReader class
string tt = read.ReadToEnd();
Console.WriteLine(tt);
}
}
}
Output:
Example #3
C# program to demonstrate usage of Read() method of StringReader class.
Code :
using System;
using System.IO;
namespace Program
{
class Check
{
//Calling the main method
static void Main(string[] args)
{
//A string variable holding a string of characters is defined
var tt = "Welcome to StringReader class.";
//an instance of the stringreader class is created
var read = new StringReader(tt);
//a counter is declared and initialized to zero
int count1 = 0;
//the occurrence of the character to be identified from the statement is assigned to a character variable
char ch = 'e';
//an integer variable is declared
int x;
//Read() method is called and checked if the control has not reached the end of the string or if the string do not exist
while ((x = read.Read()) != -1)
{
char ct = (char) x;
//if the character whose occurrence must be counted is same as the character in the statement while traversing through the statement, the counter is incremented by one
if (ct.Equals(ch))
{
count1++;
}
}
//finally the number of occurrences of the specified character is displayed
Console.WriteLine($"The number of '{ch}' characters in the given statement is {count1}");
}
}
}
Output:
Conclusion
In this tutorial, we understand the concept of StringReader class in C# through definition, constructors of StringReader class, and methods of StringReader class, working of StringReader class through programming examples and their outputs demonstrating the methods of StringReader class.
Recommended Articles
This is a guide to C# StringReader. Here we discuss a brief overview on C# StringReader and its working along with different Examples and Code Implementation. You can also go through our other suggested articles to learn more –