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
|
|
|
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.
| Show full unit code | 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.
| Show full unit code | 4 chars read : Hell
4 chars read : o cr
4 chars read : uel
4 chars read : Worl
1 chars read : d
|
|
|
|