德尔福:换行的字符值

时间:2015-11-19 07:08:01

标签: delphi delphi-xe2

我有一个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;

1 个答案:

答案 0 :(得分:3)

您无需使用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