Delphi : 换行符的字符值

Delphi : Char value for newline

我有一个 AnsiString 如下:

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 

如何在名为 Users 的 TStringlist 中添加新行的分隔符,以便我得到:

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

这是我的代码:

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;

您不需要为此使用 DelimitedText。您可以简单地直接分配给 Text,这会将换行符视为分隔符。

{$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.

输出:

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