2014-05-10 43 views
0

所需的數據我在串是6/8或有時更多行這樣的數據:Delphi的拷貝從串

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" 

i。從第二需要合計/最後一行/第一和第四字從每行是這樣的:

從總線I需要從每一行的第一和第四字

0dtm /雜項 // < - 所需的數據

總數量的行

2dtm/every // <同樣的事情 - 需要的數據

注:行數並不總是相同

的數線不總是相同的,所以我不能使用複製funstion 任何其他建議?

thx

回答

1

文本格式看起來非常嚴格。假設數據可以逐行處理,看起來您可以使用預先確定的字符索引來解析每一行。

我會先寫代碼解析單行記錄。

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; 
+0

真棒例如工作完美THX很多(Y) – dudey

1

讓我們的字符串在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;