C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
With StringReader, we read lines from a string individually in the order they appear. This type enables us to access string data through a stream-oriented interface.
Notes: StringReader can read parts of strings in an intuitive way. It can lead to performance improvements over Split.
Example. First, the StringReader is a class found in the System.IO namespace. Please specify that when you try to use StringReader. The instance constructor receives a string parameter. This is stored internally as a field in the StringReader.
Here: We use the ReadLine method. This returns a newly-allocated and copied substring based on the current position.
C# program that uses StringReader using System; using System.IO; class Program { const string _input = @"Dot Net Perls is a website you are reading"; static void Main() { // Creates new StringReader instance from System.IO using (StringReader reader = new StringReader(_input)) { // Loop over the lines in the string. int count = 0; string line; while ((line = reader.ReadLine()) != null) { count++; Console.WriteLine("Line {0}: {1}", count, line); } } } } Output Line 1: Dot Net Perls Line 2: is a website Line 3: you are reading
In this program, we use a const string from the metadata to instantiate the StringReader instance. We can then read each line individually from the string data, in a way similar to reading a file with StreamReader.
String LiteralUsing StreamReader
Next steps. The while-loop uses an embedded assignment and checks the string variable result for null to detect the end of file condition. The output shows that each line was retrieved.
Read blocks. Read() can be used to populate a character array (a block) based on a range of the StringReader buffer. The buffer must be allocated before you call the method. Internally, this uses the CopyTo instance method on the string type.
Internals. The internal representation of the StringReader is a class with a member field string, an integer position, and an integer length. All the logic on the StringReader is based on the values of these three fields.
ReadLine internals. The ReadLine method on StringReader simply scans for the next newline starting at the current position, and then returns a substring based on the field string.
Tip: StringReader internally stores only one string. It allocates and copies new strings each time you call Read methods.
Benchmark. How efficient is the StringReader type, particularly in comparison to the Split method? We ran a simple benchmark that separates into lines a string literal with several newline characters in it.
The two methods, Method1 and Method2, both count the number of non-newline characters in the lines. The result was that StringReader was much faster than Split. This is likely because no arrays were needed.
And: With StringReader, each line could be brought into memory as a string object as we go along.
C# program that tests StringReader and Split using System; using System.Diagnostics; using System.IO; class Program { static int Method1(string data) { int count = 0; using (StringReader reader = new StringReader(data)) { while (true) { string line = reader.ReadLine(); if (line == null) { break; } count += line.Length; } } return count; } static int Method2(string data) { int count = 0; string[] lines = data.Split(new char[] { '\n' }); foreach (string line in lines) { count += line.Length; } return count; } static void Main() { const string data = "Dot\nNet\nPerls\nIs a website\nDo you like it?"; Console.WriteLine(Method1(data)); Console.WriteLine(Method2(data)); const int max = 1000000; var s1 = Stopwatch.StartNew(); for (int i = 0; i < max; i++) { Method1(data); } s1.Stop(); var s2 = Stopwatch.StartNew(); for (int i = 0; i < max; i++) { Method2(data); } s2.Stop(); Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / max).ToString("0.00 ns")); Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / max).ToString("0.00 ns")); Console.Read(); } } Result 38 38 314.09 ns (StringReader) 374.67 ns (Split)
Tip: If you need to separate a string into lines and do not need an array, the StringReader may be more efficient than Split.
Summary. On the StringReader type, we invoked the ReadLine method and constructor. StringReader provides methods that are stream-oriented, enabling you to use a string as a file stream. ReadLine internally allocates new string instances.
And: StringReader is useful for looping over the lines in a string stored as a string literal.