StringReader
Class
Definition
Implements a TextReader that reads from a string.
[System.Runtime.InteropServices.ComVisible(true)]
public class StringReader : System.IO.TextReader
- Inheritance
- Attributes
Inherited Members
System.IO.TextReader
System.MarshalByRefObject
System.Object
Examples
The following example shows how to read an entire string asynchronously.
using System;
using System.IO;
using System.Text;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
ReadCharacters();
}
static async void ReadCharacters()
{
StringBuilder stringToRead = new StringBuilder();
stringToRead.AppendLine("Characters in 1st line to read");
stringToRead.AppendLine("and 2nd line");
stringToRead.AppendLine("and the end");
using (StringReader reader = new StringReader(stringToRead.ToString()))
{
string readText = await reader.ReadToEndAsync();
Console.WriteLine(readText);
}
}
}
}
// The example displays the following output:
//
// Characters in 1st line to read
// and 2nd line
// and the end
//
Imports System.IO
Imports System.Text
Module Module1
Sub Main()
ReadCharacters()
End Sub
Async Sub ReadCharacters()
Dim stringToRead = New StringBuilder()
stringToRead.AppendLine("Characters in 1st line to read")
stringToRead.AppendLine("and 2nd line")
stringToRead.AppendLine("and the end")
Using reader As StringReader = New StringReader(stringToRead.ToString())
Dim readText As String = Await reader.ReadToEndAsync()
Console.WriteLine(readText)
End Using
End Sub
End Module
' The example displays the following output:
'
' Characters in 1st line to read
' and 2nd line
' and the end
'
Remarks
StringReader enables you to read a string synchronously or asynchronously. You can read a character at a time with the Read or the ReadAsync method, a line at a time using the ReadLine or the ReadLineAsync method and an entire string using the ReadToEnd or the ReadToEndAsync method.
Note
This type implements the IDisposable interface, but does not actually have any resources to dispose. This means that disposing it by directly calling Dispose() or by using a language construct such as using (in C#) or Using (in Visual Basic) is not necessary.
The following table lists examples of other typical or related I/O tasks.
| To do this... | See the example in this topic... |
|---|---|
| Create a text file. | How to: Write Text to a File |
| Write to a text file. | How to: Write Text to a File |
| Read from a text file. | How to: Read Text from a File |
| Append text to a file. | How to: Open and Append to a Log File File.AppendText FileInfo.AppendText |
| Get the size of a file. | FileInfo.Length |
| Get the attributes of a file. | File.GetAttributes |
| Set the attributes of a file. | File.SetAttributes |
| Determine if a file exists. | File.Exists |
| Read from a binary file. | How to: Read and Write to a Newly Created Data File |
| Write to a binary file. | How to: Read and Write to a Newly Created Data File |
Constructors
| StringReader(String) |
Initializes a new instance of the StringReader class that reads from the specified string. |
Methods
| Close() |
Closes the StringReader. |
| Dispose(Boolean) |
Releases the unmanaged resources used by the StringReader and optionally releases the managed resources. |
| Peek() |
Returns the next available character but does not consume it. |
| Read() |
Reads the next character from the input string and advances the character position by one character. |
| Read(Char[], Int32, Int32) |
Reads a block of characters from the input string and advances the character position by |
| ReadAsync(Char[], Int32, Int32) |
Reads a specified maximum number of characters from the current string asynchronously and writes the data to a buffer, beginning at the specified index. |
| ReadBlockAsync(Char[], Int32, Int32) |
Reads a specified maximum number of characters from the current string asynchronously and writes the data to a buffer, beginning at the specified index. |
| ReadLine() |
Reads a line of characters from the current string and returns the data as a string. |
| ReadLineAsync() |
Reads a line of characters asynchronously from the current string and returns the data as a string. |
| ReadToEnd() |
Reads all characters from the current position to the end of the string and returns them as a single string. |
| ReadToEndAsync() |
Reads all characters from the current position to the end of the string asynchronously and returns them as a single string. |