String delphi从字符串复制所需数据

String delphi从字符串复制所需数据,string,delphi,copy,String,Delphi,Copy,我有6/8或更多行的字符串数据,如下所示: part: size actsize name 0dtm: 000a0000 00020000 "misc" 1dtm: 00480000 00020000 "every" 2dtm: 00300000 00020000 "hexs" 3dtm: 0fa00000 00020000 "stem" 4dtm: 02800000 00020000 "ache" 5dtm: 093a0000 00020000 "data" 我需要从第二行到最后

我有6/8或更多行的字符串数据,如下所示:

part:    size   actsize  name
0dtm: 000a0000 00020000 "misc"
1dtm: 00480000 00020000 "every"
2dtm: 00300000 00020000 "hexs"
3dtm: 0fa00000 00020000 "stem"
4dtm: 02800000 00020000 "ache"
5dtm: 093a0000 00020000 "data"
我需要从第二行到最后一行,每行的第一个和第四个单词如下:

part:    size   actsize  name
0dtm: 000a0000 00020000 "misc"
1dtm: 00480000 00020000 "every"
2dtm: 00300000 00020000 "hexs"
3dtm: 0fa00000 00020000 "stem"
4dtm: 02800000 00020000 "ache"
5dtm: 093a0000 00020000 "data"
从总行中,我需要每行的第一个和第四个单词


0dtm/misc/让我们看看您的字符串是在TStringList还是字符串数组中。您可以使用TStrings.CommaText或DelimitedText属性提取字符串的某些部分:

TempList := TStringList.Create; // helper list
for i := 0 to List.Count - 1 do begin //or to High(array)
  TempList.CommaText := List[i];
  if TempList.Count >= 4 then begin //use separated elements
    FirstData := TempList[0];
    FourthData := TempList[3];
  end;
end;
TempList.Free;

文本格式似乎非常严格。假设可以逐行处理数据,看起来可以使用预先确定的字符索引来解析每一行

我首先编写代码将一行解析为一条记录

type
  TItem = record
    Part: string;
    Size: Integer;
    ActSize: Integer;
    Name: string;
  end;

function GetItemFromText(const Text: string): TItem;
begin
  Result.Part := Copy(Text, 1, 4);
  Result.Size := StrToInt('$'+Copy(Text, 7, 8));
  Result.ActSize := StrToInt('$'+Copy(Text, 16, 8));
  Result.Name := Copy(Text, 26, Length(Text)-26);
end;
一旦我们掌握了这一点,处理您的数据就很简单了。首先,将其加载到字符串列表中,作为一种将其解析为单独行的方法

var
  Items: TStringList;
....
Items := TStringList.Create;
Items.Text := MyData;
然后,处理每一行,记住跳过标题的第一行:

var
  i: Integer;
....
for i := 1 to Items.Count-1 do 
begin
  Item := GetItemFromText(Items[i]);
  // do whatever needs to be done with the content of Item
end;