Advertisement
Constructors | Description |
---|---|
FileStream(String string) | The constructor created an object of the StringReader that reads from the specified String object. |
Methods | Description |
---|---|
int Read() | This method reads the next character from the input string as an Int32 object, or -1 if no more characters are available. |
int Read(char[] array, int index, int total) | This method reads a character array from the input string, where:
This method returns the number of characters that have been read from the input string. |
String ReadLine() | This method reads a line of characters from the input string and returns the data as a String, or null if the end of the input stream is reached. |
void Close() | This method closes the StringReader. |
//C# Example of StringReader
using System;
using System.IO;
class A
{
public static void Main(String[] ar)
{
try
{
//A character value
char ch = 'A';
//A character array
char[] arr = {'B', 'l', 'u', 'e', ' ', 'M' ,'o', 'o', 'n'};
//Creating a String object
String str = "The sky is limitless";
//Creating an object of StringWriter
//to write to characters to the string of type StringBuilder
//automatically created by the StringWriter class
StringWriter sw = new StringWriter();
Console.WriteLine("Writing to the character to a string by using StringWriter");
//Writing a char
sw.Write(ch);
//Writing a part of char array
//Starting at its zeroth-index and a total of 4 characters
sw.Write(arr, 0 , 7);
//Writing a String
sw.Write(str);
Console.WriteLine("Characters are successfully written to a string by StringWriter");
//Extracting the content of string created by StringWriter class
//By calling the ToString() method on it
String str_from_sb = sw.ToString();
//Closing the stream i.e. StringWriter stream
//sw.Close();
//Creating an object of StringReader
//to read the characters from string of type StringBuilder
//which was written to by StringWriter class.
//And now it is converted to a String, str
StringReader sr = new StringReader(str_from_sb);
int c;
Console.WriteLine("Reading the characters of the input string by StringReader:");
while((c = sr.Read()) != -1)
{
Console.Write((char)c);
}
//Closing the StringReader
sr.Close();
}
catch(IOException e)
{
Console.WriteLine(e);
}
}
}
Writing to the character to a string by using StringWriter
Characters are successfully written to a string by StringWriter
Reading the characters of the input stream by StringReader:
ABlue MoThe sky is limitless
Advertisement
//C# Example of StringReader
using System;
using System.IO;
class A
{
public static void Main(String[] ar)
{
try
{
//A character value
char ch = 'A';
//A character array
char[] arr = {'B', 'l', 'u', 'e', ' ', 'M' ,'o', 'o', 'n'};
//Creating a String object
String str = "The sky is limitless";
//Creating an object of StringWriter
//to write to characters to the string of type StringBuilder
//automatically created by the StringWriter class
StringWriter sw = new StringWriter();
Console.WriteLine("Writing to the character to a string by using StringWriter");
//Writing a char
sw.Write(ch);
//Writing a part of char array
//Starting at its zeroth-index and a total of 4 characters
sw.Write(arr, 0 , 7);
//Writing a String
sw.Write(str);
Console.WriteLine("Characters are successfully written to a string by StringWriter");
//Extracting the content of string created by StringWriter class
//By calling the ToString() method on it
String str_from_sb = sw.ToString();
//Closing the stream i.e. StringWriter stream
//sw.Close();
//Creating an object of StringReader
//to read the characters from string of type StringBuilder
//which was written to by StringWriter class.
//And now it is converted to a String, str
StringReader sr = new StringReader(str_from_sb);
Console.WriteLine("Reading a line of characters from the input string using StringReader:");
String s;
//Calling the ReadLine() method which returns the String that is read
//It returns null if there is nothing to read anymore
while((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
//Closing the StringReader
sr.Close();
}
catch(IOException e)
{
Console.WriteLine(e);
}
}
}
Writing to the character to a string by using StringWriter
Characters are successfully written to a string by StringWriter
Reading a line of characters from the input string using StringReader:
ABlue MoThe sky is limitless
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement