delphi中的字符串拆分函数

时间:2009-12-18 19:12:03

标签: delphi string list

实际上我之前已经打开了问题,

但无法得到我想要的答案,所以我想再次感谢所有

例如,我有一些文本文件名是'test.txt',内部文本内容看起来像

hello all
good day
happy is

我想修改以下来源,从'hello all'的第一个索引进行迭代,我的意思是..

如果我点击showmessage(第一个)然后我想在test.txt文件中得到'hello',

如果点击showmessage(第二个)然后想要'全部'并继续,

如果我再次点击showmessage(第一个)然后想要'好'和

再次点击showmessage(第二个),然后想要获得我想要的'日'。

提前致谢!并感谢所有帮助过我的人!

procedure TForm1.BitBtn1Click(Sender: TObject);
var
  list : TStringList;
  first, second, third: string;
begin
  list := TStringList.Create;
  try
    list.Delimiter := #32;
    list.LoadFromFile('test.txt');
    first := list[0];
    second := list[1];
    ShowMessage(first);
    ShowMessage(second);
  finally
    list.Free;
  end;
end;

你好你可以修改如下吗?

我想使用showmessage(第一个)和showmessage(两个),如果非常欣赏的话!

procedure TForm1.BitBtn1Click(Sender: TObject);

var
  theFileStuff : tstringList;
  oneLine      : tStringList;
  x,y          : integer;
begin
    theFileStuff      := tStringList.Create;
    oneLine           := tStringList.create;
    oneLine.Delimiter := #32;
    theFileStuff.LoadFromFile('test.txt');
    for x := 0 to theFileStuff.count-1 do
    begin
          oneLine.DelimitedText := theFileStuff[x];
          for y := 0 to oneLine.count-1
          do
          //ShowMessage(oneLine[y]);
          ShowMessage(first);
          ShowMessage(second);

    end;
    oneLine.Free;
    theFileStuff.Free;
end;

2 个答案:

答案 0 :(得分:3)

试试这个

procedure TForm1.ShowFields(Sender: TObject);
var
  theFileStuff : tstringList;
  oneLine      : tStringList;
  x,y          : integer;
begin
    theFileStuff      := tStringList.Create;
    oneLine           := tStringList.create;
    oneLine.Delimiter := #32;
    theFileStuff.LoadFromFile('fileName');
    for x := 0 to theFileStuff.count-1 do
    begin
          oneLine.DelimitedText := theFileStuff[x];
          for y := 0 to oneLine.count-1
          do ShowMessage(oneLine[y]);
    end;
    oneLine.Free;
    theFileStuff.Free;
end;

如果您知道每行只有两个项目,则可以替换以下代码:

for y := 0 to oneLine.count-1
do ShowMessage(oneLine[y])

ShowMessage(oneLine[0]);    // First
ShowMessage(oneLine[1]);    // Second

我的代码更通用,每行处理任意数量的项目

答案 1 :(得分:2)

Delimiter属性仅在使用DelimitedText属性时有意义。您将不得不使用2个单独的TStringList对象来满足您的要求,例如:

var
  list, values : TStringList;
  curListIdx, curValueIdx: Integer;

procedure TForm1.FormCreate(Sender: TObject);
begin
  curListIdx := -1;
  curValueIdx := -1;
  list := TStringList.Create; 
  values := TStringList.Create;
  values.Delimiter := #32;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  list.Free;
  values.Free;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
var
  S: String;
begin 
  if curListIdx = -1 then
  begin
    list.LoadFromFile('test.txt');
    if list.Count = 0 then Exit;
    curListIdx := 0;
  end;

  if curValueIdx = -1 then
  begin
    if curListIdx = list.Count then
    begin
      curListIdx := -1;
      Exit;
    end;
    values.DelimitedText := list[curListIdx];
    Inc(curListIdx);
    if values.Count = 0 then Exit;
    curValueIdx := 0;
  end;

  S := values[curValueIdx]; 
  Inc(curValueIdx)
  if curValueIdx = values.Count then curValueIdx := -1;

  ShowMessage(S);
end;