2012-04-06 22 views
2

我將如何使用Delphi從特定字符串之後的備註字段中刪除數據,例如我正在通過它顯示的數據庫中的數據:從備註字段的內容中刪除特定行

<Data I want to keep> 

======= Old Data ======== 
<line 1> 
<line 2> 
etc. 

我怎麼能告訴德爾福刪除舊數據行(包括)後的所有數據?但不要觸摸我想保留的數據?

回答

8

類似:

var 
    I: Integer; 
    s: string; 
begin 
    s := 'your big string with ======= Old Data ======== and more'; 
    I:=Pos('======= Old Data ========',s); 
    if I>0 then 
    Delete(s, I, MaxInt); 
    ShowMessage(s); 
+2

+1一個更快的解決方案可能是'如果我> 0,那麼SetLength(S,I - 1);',雖然。至少,它應該是'System.Delete',尤其是因爲問題是關於數據庫。有人不小心用'with'語句犯了錯誤並刪除了數據(或者最終難以解決新手的編譯錯誤)。 – 2012-04-07 01:43:36

+0

何時何地通話? – NGLN 2012-04-07 09:01:30

1

試試這個:

procedure myForm.ClearFromLine(value: string); 
var 
    i, index: integer; 
begin 
    index := memo.lines.IndexOf(value); 
    if index = -1 then 
    Exit; 
    memo.lines.BeginUpdate; 
    try 
    for i := memo.lines.count - 1 downto index do 
     memo.lines.delete(i); 
    finally 
    memo.lines.EndUpdate; 
    end; 
end; 
+4

不是downvoting,但問題是關於備忘*字段*,而不是'TMemo'。 :) – 2012-04-07 00:24:22