![]() |
|
#1
|
|||
|
|||
|
How to replace all text with one click on the button
without seeing the replace dialog. Here's the problem! i've got an URL ....com/bla.cgi?bla=*|bla=-|bla=` and now i want to replace these characters *,-,` with some text like this * = test111 - = test222 ` = test333 That text is writen in label1,2 and 3. the URL is writen in Memo1! How to replace these characters with one click on the button without seeing the replace dialog? can someone help me with these cause i searched the help but these to complicated for me cause im begginer... thx for help if anyone... Anze |
|
#2
|
|||
|
|||
|
Simply write a "ReplateText" procedure. Here is an example.
procedure ReplaceText(var Text : string; Search, Replace : string); var p : integer; begin repeat p := pos(Search, Text); if p <> 0 then begin Text := copy(Text, 1, p - 1) + Replace + copy(Text, p + Length(Search), Length(Text)); end; until p = 0; end; This simple procedure has a major flaw: it can go on forever in certain circumstances. If, for example, Search = 'A' and Replace = 'AA', it will loop forever if Text contains an 'A'. To overcome this problem, one solution is to fill a list of all occurences of Search in a first pass, and replace everything from end to beginning in a second pass like: procedure ReplaceText2(var Text : string; Search, Replace : string); var i, p : integer; positions : array of integer; begin SetLength(positions, 0); i := 0; repeat inc(i); if copy(Text, i, Length(Search)) = Search then begin SetLength(Positions, Length(Positions) + 1); Positions[Length(Positions) - 1] := i; i := i + Length(Search) - 1; end; until i = Length(Text); for i := Length(Positions) - 1 downto 0 do Text := copy(Text, 1, Positions[i] - 1) + Replace + copy(Text, Positions[i] + Length(Search), Length(Text)); end; |
|
#3
|
|||
|
|||
|
If you are using Delphi 4 or higher, use the StringReplace function.
|
|
#4
|
|||
|
|||
|
If you change the Memo to a RichEdit, you can use this code.
It is based on the simple syntax highlighter I posted serveral weeks ago: {Richedit Text: This application execute a illegal operation... (02/12/01) nilce RE: This application execute a illegal operation... (02/12/01) Claude User-login names Aliases (02/12/01) Keito How does FormKeyDown gets the shortcut "Alt-F6" ? (02/12/01) Petra RE: How does FormKeyDown gets the shortcut "Alt-F6" ? (02/12/01) BlakeBlake Replacing text... different characters with one click...?? (02/12/01) Element5 RE: Replacing text... different characters with one click...?? (02/12/01) Claude RE: Replacing text... different characters with one click...?? (02/12/01) douglas Monitoring traffic on network interfaces (02/12/01) AlexBelgium Easy way to change creation order of VISUAL components? (02/12/01) renestam RE: Easy way to change creation order of VISUAL components? (02/12/01) Claude Scrool Tree (02/12/01) YED } procedure ReplaceAllWords(RE: TRichEdit; Search, Replace: String; SearchType: TSearchTypes); var i, CharPos, CharPos2, noChars: Integer; begin CharPos := 0; noChars := 0; for i := 0 to Pred(RE.Lines.Count) do noChars := noChars + Length(RE.Lines[i]); repeat CharPos2 := RE.FindText(Search, CharPos, noChars, SearchType); CharPos := CharPos2+1; if CharPos > 0 then begin RE.SelLength := 0; RE.SelText := ''; RE.SelStart := CharPos2; RE.SelLength := Length(TheWord); RE.SelText := Replace; end; until charpos = 0; end; procedure TForm1.Button1Click(Sender: TObject); begin { Try the different options to get the effects. []: replaces all occurances of Search with replace, regardless of case or if word is whole. [stWholeWord]: replaces the whole word [stMatchCase]: replaces the chars in Search with chars in Replace only if the case matches [stMatchCase, stWholeWord]: replaces the whole word only if the case matches. } // This replaces all occurances of RE: with SV: ReplaceAllWords(RichEdit1, 'RE:', 'SV:', [stWholeWord]); end; |
![]() |
| Thread Tools | |
| Display Modes | |
|
|