Veela
Veela

Reputation: 280

Delphi : Char value for newline

I have an AnsiString as follows :

UID,CardNo,Pin,Password,Group,StartTime,EndTime,Name,SuperAuthorize
1,0,1,,0,0,0,Johnny Test,0
2,0,2,,0,0,0,Harry Potter,0
3,0,3,,0,0,0,Scooby Doo,0
4,0,4,,0,0,0,Baby Lonney Toons,0 

How do I add the delimiter for new line in a TStringlist named Users so that I get :

Users[1] : 1,0,1,,0,0,0,Johnny Test,0
Users[2] : 2,0,2,,0,0,0,Harry Potter,0
Users[3] : 3,0,3,,0,0,0,Scooby Doo,0

Here's my code :

procedure TFRTest.GetInfo(sbuffer : AnsiString);
var
    userIndex : Integer;;
    Users  : TStringList;
begin

    Users := TStringList.Create;
    Users.Delimiter := Char(10) ;
    try
         Users.DelimitedText :=  sbuffer;

         for userIndex := 0 to Users.Count -1 do
         begin
             ShowMessage(Users[userIndex]);
         end;
    finally
         Users.Free;
    end;

Upvotes: 1

Views: 119

Answers (1)

David Heffernan
David Heffernan

Reputation: 612794

You do not need to use DelimitedText for this. You can simply assign directly to Text and that will treat line breaks as delimiters.

{$APPTYPE CONSOLE}

uses
  Classes;

const
  Str =
    'UID,CardNo,Pin,Password,Group,StartTime,EndTime,Name,SuperAuthorize' + sLineBreak +
    '1,0,1,,0,0,0,Johnny Test,0' + sLineBreak +
    '2,0,2,,0,0,0,Harry Potter,0' + sLineBreak +
    '3,0,3,,0,0,0,Scooby Doo,0' + sLineBreak +
    '4,0,4,,0,0,0,Baby Lonney Toons,0';

var
  i: Integer;
  StringList: TStringList;

begin
  StringList := TStringList.Create;
  try
    StringList.Text := Str;

    Writeln(StringList.Count);
    for i := 0 to StringList.Count-1 do begin
      Writeln(StringList[i]);
    end;
  finally
    StringList.Free;
  end;

  Readln;
end.

Output:

5
UID,CardNo,Pin,Password,Group,StartTime,EndTime,Name,SuperAuthorize
1,0,1,,0,0,0,Johnny Test,0
2,0,2,,0,0,0,Harry Potter,0
3,0,3,,0,0,0,Scooby Doo,0
4,0,4,,0,0,0,Baby Lonney Toons,0

Upvotes: 3

Related Questions