Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
delphi中的字符串分割函数_Delphi_String_List - Fatal编程技术网

delphi中的字符串分割函数

delphi中的字符串分割函数,delphi,string,list,Delphi,String,List,其实我之前有个问题 但是我无法得到我想要的答案,所以我想再问一次,谢谢大家 例如,我有一些文本文件名为“test.txt”,其中的文本内容如下 hello all good day happy is 我想修改下面的源代码,从“hello all”的第一个索引开始迭代 如果我单击showmessage(首先),那么我希望在test.txt文件中获取“hello” 如果单击showmessage(第二个),则希望获得“全部”并继续 如果我再次单击showmessage(第一个),则希望获得“良好”

其实我之前有个问题

但是我无法得到我想要的答案,所以我想再问一次,谢谢大家

例如,我有一些文本文件名为“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;

试试这个

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])


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

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])


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

Delimiter属性仅在使用DelimitedText属性时才有意义。您需要使用两个单独的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;

Delimiter属性仅在使用DelimitedText属性时才有意义。您需要使用两个单独的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;

你好,谢谢你的帮助!我想修改如下,你能帮我吗,你可以用一个TStringList(SL)和一个字符串变量。SL.LoadFromFile(),将SL.Text分配给变量,然后在设置SL.Delimiter(可能还有SL.StrictDelimiter:=True)后将变量重新分配回SL.DelimitedText。您好,谢谢您的帮助!我想修改如下,你能帮我吗,你可以用一个TStringList(SL)和一个字符串变量。SL.LoadFromFile(),将SL.Text分配给变量,然后在设置SL.Delimiter(可能还有SL.StrictDelimiter:=True)后将变量重新分配回SL.DelimitedText。感谢您的帮助!我被修改了一些你能检查一下吗?再次感谢你的帮助!我被修改了一些你能检查一下吗?谢谢again@Paul:您当然可以想办法将“ShowMessage(oneline[y]);”改为“first:=oneline[0];second:=oneline[1];ShowMessage(first);ShowMessage(second);”。你需要试着自己解决问题,不要指望每个人都为你做每件事。你永远也学不会那样。大家好,谢谢!我下定决心了,如果没有别人的帮助,也许我无法解决它,我真的很感激!再次感谢:)@Paul:您当然可以想办法将“ShowMessage(oneline[y]);”改为“first:=oneline[0];second:=oneline[1];ShowMessage(first);ShowMessage(second);”。你需要试着自己解决问题,不要指望每个人都为你做每件事。你永远也学不会那样。大家好,谢谢!我下定决心了,如果没有别人的帮助,也许我无法解决它,我真的很感激!再次感谢:)