Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 02-12-2001, 03:32 AM
Element5 Element5 is offline
Junior Member
 
Join Date: Feb 2001
Posts: 14
Default Replacing text... different characters with one click...??

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
Reply With Quote
  #2  
Old 02-12-2001, 05:58 AM
Claude Claude is offline
Senior Member
 
Join Date: Jan 2001
Posts: 178
Default RE: Replacing text... different characters with one click...??

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;
Reply With Quote
  #3  
Old 02-12-2001, 07:58 AM
douglas douglas is offline
Senior Member
 
Join Date: Jul 2001
Posts: 7,695
Default RE: Replacing text... different characters with one click...??

If you are using Delphi 4 or higher, use the StringReplace function.
Reply With Quote
  #4  
Old 02-12-2001, 10:46 AM
MrBaseball34 MrBaseball34 is offline
Senior Member
 
Join Date: Jan 2001
Posts: 7,260
Default RE: Replacing text... different characters with one click...??

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;
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 08:07 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.