2009-09-28 96 views
1

出於某種原因,使用德爾福SEARCHTEXT問題

SearchText := 'Program Files'; 
    ReplaceText := 'Program Files (x86)'; 
    SearchAndReplace(SearchText, ReplaceText); 

會什麼都不做,它只是將不會更改文本,使用其他任何文本時工作正常。

這是某種「保留」字嗎?或()是什麼讓它不起作用?

procedure Tfc_Great.SearchAndReplace 
      (InSearch, InReplace: string) ; 
var X, ToEnd : integer; 
    oldCursor : TCursor; 
begin 
    oldCursor := Screen.Cursor; 
    Screen.Cursor := crHourglass; 
    with RichEdit1 do 
    begin 
    X := 0; 
    ToEnd := length(Text) ; 
    X := FindText(inSearch, X, ToEnd, []) ; 
    while X <> -1 do 
    begin 
     SetFocus; 
     SelStart := X; 
     SelLength := length(inSearch) ; 
     SelText := InReplace; 
     X := FindText(inSearch, 
        X + length(InReplace), 
        ToEnd, []) ; 
    end; 
    end; 
    Screen.Cursor := oldCursor; 
end; 
+0

您正在使用的德爾福的版本? – 2009-09-28 12:00:37

+0

更廣泛的背景是什麼?我從來沒有聽說過SearchAndReplace函數,我找不到任何helpfile條目。這是組件的一部分嗎?我通常使用StringReplace進行搜索和替換操作。 – 2009-09-28 12:10:10

+0

我不明白。你打算修改哪個字符串? – 2009-09-28 12:10:35

回答

1

嘗試分配輸出;)

SearchText := 'Program Files'; 
ReplaceText := 'Program Files (x86)'; 
ResultText := SearchAndReplace(Text, SearchText, ReplaceText); 

function SearchAndReplace 
    (sSrc, sLookFor, sReplaceWith : string) : string; 
var 
    nPos, nLenLookFor : integer; 
begin 
    nPos := Pos(sLookFor, sSrc) ; 
    nLenLookFor := Length(sLookFor) ; 
    while (nPos > 0) do begin 
    Delete(sSrc, nPos, nLenLookFor) ; 
    Insert(sReplaceWith, sSrc, nPos) ; 
    nPos := Pos(sLookFor, sSrc) ; 
    end; 
    Result := sSrc; 
end; 
+0

我正在使用 RichEdit.Lines.SaveToFile(mfp,TEncoding.ASCII); – Tom 2009-09-28 12:19:54

+0

RichEdit.Lines.LoadFromFile(mfp,TEncoding.ASCII); – Tom 2009-09-28 12:40:38

+0

RichEdit中有什麼?你想要替換的文字?什麼是mfp? – Scoregraphic 2009-09-28 13:19:42