2009-09-28 97 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