Delphi:将特定行从 Stringlist 复制到另一个 [英] Delphi : Copy specific lines from Stringlist to another

查看:29
本文介绍了Delphi:将特定行从 Stringlist 复制到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试按索引将特定字符串从一个 TStringList 复制到另一个时,出现索引限制超出债券"错误.

我有一个文本文件,其中包含用管道|"格式化的行分隔符.它看起来像这样:

在我的目标文件中,我只想从以 '3M' 开头的行中复制一些项目,以获得如下内容(例如第一行):

<前>3M 2189300002 12.99

3MStringlist.strings[1]

2189300002Stringlist.strings[3]

12.99Stringlist.strings[6]

这是我的代码:

varsl,new : tstringlist;lscount,index : 整数;开始sl:= TStringList.Create;sl.LoadFromFile('C:\Folder\test.txt');新:= tstringlist.create;lscount := lstringlist.Count;for index := 0 to lscount do开始sl.delimiter := '|';sl.StrictDelimiter := True;sl.DelimitedText := sl.Strings[index];如果 sl.Strings[1] = '3M' 那么new.Add(sl.Strings[1]+sl.Strings[3]+sl.Strings[6]);结尾;new.SaveToFile('C:\Folder\new.txt');sl.免费;新的.免费的结尾;

我的代码有什么问题?

解决方案

您的代码中有很多错误.

您的 for 循环从索引 0 循环到 lscount,但 TStringList 的上限是 lscount-1 代替.

您在循环遍历 sl 时正在修改 sl.解析每一行时需要使用单独的TStringList.

您正在使用基于 1 的索引访问解析后的字符串,但 TStringList 使用基于 0 的索引.

尝试更像这样的事情:

varsl,解析,新:TStringList;索引:整数;开始sl := TStringList.Create;尝试sl.LoadFromFile('C:\Folder\test.txt');新:= TStringList.create;尝试解析:= TStringList.Create;尝试parse.Delimiter := '|';parse.StrictDelimiter := True;对于索引 := 0 到 sl.Count-1 做开始parse.DelimitedText := sl.Strings[index];如果 (parse.Count > 5) 和 (parse.Strings[0] = '3M') 那么new.Add(parse.Strings[0] + ' ' + parse.Strings[2] + ' ' + parse.Strings[5]);结尾;最后解析.免费;结尾;new.SaveToFile('C:\Folder\new.txt');最后新的.免费;结尾;最后sl.免费结尾;结尾;

I'm getting an "Index limit out of bonds" error when I try to copy specific strings by index from one TStringList to another.

I have a text file that contain lines formatted with a pipe "|" delimiter. It looks like this:

In my destination file, I want to copy only some items from lines that start with '3M', in order to get something like this (the first line, for example):

3M 2189300002 12.99

3M is Stringlist.strings[1]

2189300002 is Stringlist.strings[3]

12.99 is Stringlist.strings[6]

Here is my code:

var
  sl,new : tstringlist;
  lscount,index : integer;
begin
  sl:= TStringList.Create;
  sl.LoadFromFile('C:\Folder\test.txt');

  new := tstringlist.create;

  lscount :=  lstringlist.Count;

  for index := 0 to lscount do
  begin
    sl.delimiter := '|';
    sl.StrictDelimiter := True;

    sl.DelimitedText := sl.Strings[index];

    if sl.Strings[1] = '3M' then
      new.Add(sl.Strings[1]+sl.Strings[3]+sl.Strings[6]);

  end;

  new.SaveToFile('C:\Folder\new.txt');
  sl.Free;
  new.Free

end;

What's wrong with my code?

解决方案

There are quite a number of mistakes in your code.

Your for loop is looping from index 0 to lscount, but the upper bound of the TStringList is lscount-1 instead.

You are modifying sl while you are looping through sl. You need to use a separate TStringList when parsing each line.

You are accessing the parsed strings using 1-based indexes, but TStringList uses 0-based indexes instead.

Try something more like this:

var
  sl, parse, new : TStringList;
  index : Integer;
begin
  sl := TStringList.Create;
  try
    sl.LoadFromFile('C:\Folder\test.txt');

    new := TStringList.create;
    try
      parse := TStringList.Create;
      try
        parse.Delimiter := '|';
        parse.StrictDelimiter := True;

        for index := 0 to sl.Count-1 do
        begin
          parse.DelimitedText := sl.Strings[index];

          if (parse.Count > 5) and (parse.Strings[0] = '3M') then
            new.Add(parse.Strings[0] + ' ' + parse.Strings[2] + ' ' + parse.Strings[5]);
        end;
      finally
        parse.Free;
      end;

      new.SaveToFile('C:\Folder\new.txt');

    finally
      new.Free;
    end;
  finally
    sl.Free
  end;
end;

这篇关于Delphi:将特定行从 Stringlist 复制到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆