Home  |  Delphi .net Home  |  System.IO.StringReader  |  Read Method
  
Read  
Method  
Reads the next character or block of characters from the current string
StringReader Class
System.IO NameSpace
CF1.  Function Read : Char;
CF2.  Function Read ( Characters : Array of Char; StartIndex : Integer; Count : Integer ) : Integer;
CF : Methods with this mark are Compact Framework Compatible
Description
If the current string has not been exhausted, the next character, or block of characters are returned.
 
The string position is updated accordingly.
 
In the first syntax, the Read method returns the next string character is returned as an integer. The integer value is the numeric equivalent of the character, or -1 if the string was exhausted. Carriage return and Line-feed characters are returned by this syntax.
 
In the second syntax, an attempt is made to read up to Count characters. These characters are overlaid onto the Characters array from StartIndex. The returned integer value is the number of characters overlaid. Zero if the string was already exhausted before the read.
Notes
You must set the length of the target Character array in the second syntax, and it must have sufficient capacity to hold the returned characters.
Microsoft MSDN Links
System.IO
System.IO.StringReader
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 
 
Reading a single character at a time
program Project1;
{$APPTYPE CONSOLE}

uses
  System.IO;

var
  Reader        : System.IO.StringReader;
  Line          : String;
  CharAsInteger : Integer;

begin
  // Define our multi line string
  Line := 'Hello' + Chr(13) + Chr(10) +
          'cruel' + Chr(13) + Chr(10) +
          'World';

  // Create our String Reader
  Reader := System.IO.StringReader.Create(Line);

  // Read the characters from the string one by one
  CharAsInteger := Reader.Read;

  while CharAsInteger <> -1 do
  begin
    if CharAsInteger = 13  // Carriage Return
    then Console.Write('[CR]')
    else if CharAsInteger = 10  // Line-feed
    then Console.Write('[LF]')
    else Console.Write(Char(CharAsInteger));

    CharAsInteger := Reader.Read;
  end;

  Console.Readline;
end.
   Hello[CR][LF]cruel[CR][LF]World
Reading blocks of characters
program Project1;
{$APPTYPE CONSOLE}

uses
  System.IO;

var
  Reader           : System.IO.StringReader;
  MultiLineString  : String;
  CharArray        : Array of Char;
  CharCount        : Integer;

begin
  // Define our multi line string
  MultiLineString := 'Hello cruel World';

  // Create our String Reader
  Reader := System.IO.StringReader.Create(MultiLineString);

  // Create our target character array
  SetLength(CharArray, 4);

  // Read the characters from the string in blocks
  CharCount := Reader.Read(CharArray, 0, 4);
  while CharCount > 0 do
  begin
    Console.WriteLine(CharCount.ToString +
                      ' chars read : '   +
                      CharArray);

    // Clear the char array before assigning into it
    CharArray[0] := ' ';
    CharArray[1] := ' ';
    CharArray[2] := ' ';
    CharArray[3] := ' ';
    CharCount := Reader.Read(CharArray, 0, 4);
  end;

  Console.Readline;
end.
   4 chars read : Hell
   4 chars read : o cr
   4 chars read : uel
   4 chars read : Worl
   1 chars read : d
 
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Contact the author