Read text line by line from an in-memory string.

strings A `StringReader` gives string data the same shape as a text reader.

String Reader

secondLine
StringReaderExample.cs
Replay: real traced execution (multi-file project)
using System;
using System.IO;

class Program
{
    static void Main()
    {
        string secondLine = "beta";
        string text = "alpha\n" + secondLine + "\ngamma";

        using StringReader reader = new StringReader(text);
        string first = reader.ReadLine() ?? "";
        string second = reader.ReadLine() ?? "";

        Console.WriteLine($"first={first}");
        Console.WriteLine($"second={second}");
    }
}
using System;
using System.IO;

class Program
{
    static void Main()
    {
        string secondLine = "delta";
        string text = "alpha\n" + secondLine + "\ngamma";

        using StringReader reader = new StringReader(text);
        string first = reader.ReadLine() ?? "";
        string second = reader.ReadLine() ?? "";

        Console.WriteLine($"first={first}");
        Console.WriteLine($"second={second}");
    }
}
using System;
using System.IO;

class Program
{
    static void Main()
    {
        string secondLine = "omega";
        string text = "alpha\n" + secondLine + "\ngamma";

        using StringReader reader = new StringReader(text);
        string first = reader.ReadLine() ?? "";
        string second = reader.ReadLine() ?? "";

        Console.WriteLine($"first={first}");
        Console.WriteLine($"second={second}");
    }
}
  1. secondLine ← beta, text ← alpha beta gamma, first ← alpha, second ← beta

    5{6    static void Main()7    {8        string secondLine→ beta = "beta"; //@secondLine="delta", "omega"9        string text→ alpha
    beta
    gamma = "alpha\n" + secondLinebeta + "\ngamma";1011        using StringReader reader = new StringReader(text);12        string first→ alpha = reader.ReadLine() ?? "";13        string second→ beta = reader.ReadLine() ?? "";1415        Console.WriteLine($"first={firstalpha}");16        Console.WriteLine($"second={secondbeta}");17    }
    outputfirst=alpha
    second=beta
  1. secondLine ← delta, text ← alpha delta gamma, first ← alpha, second ← delta

    5{6    static void Main()7    {8        string secondLine→ delta = "delta";9        string text→ alpha
    delta
    gamma = "alpha\n" + secondLinedelta + "\ngamma";1011        using StringReader reader = new StringReader(text);12        string first→ alpha = reader.ReadLine() ?? "";13        string second→ delta = reader.ReadLine() ?? "";1415        Console.WriteLine($"first={firstalpha}");16        Console.WriteLine($"second={seconddelta}");17    }
    outputfirst=alpha
    second=delta
  1. secondLine ← omega, text ← alpha omega gamma, first ← alpha, second ← omega

    5{6    static void Main()7    {8        string secondLine→ omega = "omega";9        string text→ alpha
    omega
    gamma = "alpha\n" + secondLineomega + "\ngamma";1011        using StringReader reader = new StringReader(text);12        string first→ alpha = reader.ReadLine() ?? "";13        string second→ omega = reader.ReadLine() ?? "";1415        Console.WriteLine($"first={firstalpha}");16        Console.WriteLine($"second={secondomega}");17    }
    outputfirst=alpha
    second=omega