http://www.delphibasics.co
is what you are after
Main Topics
Browse All Topics Hi!
I want to replace a ; in a string with space or some other character where the pattern is like aa;bb or ab;ab.
That is the last 2 characters and the first 2 characters must be in the alphabet (non-number).
Regards,
Tomas Helgi
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
http://www.delphibasics.co
is what you are after
function StringReplace ( const SourceString, OldPattern, NewPattern : string; Flags : TReplaceFlags ) : string;
The StringReplace function replaces the first or all occurences of a substring OldPattern in SourceString with NewPattern according to Flags settings.
The changed string is returned.
The Flags may be none, one, or both of these set values:
rfReplaceAll : Change all occurrences
rfIgnoreCase : Ignore case when searching
so in your case
before = 'AB;CD';
after = StringReplace ( before , ';', ' ', [rfReplaceAll] );
after will then be 'AB CD'
David
Lets make this more intereseting lets create our own StringReplace
function rep_str(s_in: string; rep: char; rep_w: char): string;
var
i: integer;
begin
i := 1;
result := '';
while i <> length(s_in) do
begin
if s_in[i] = rep then
begin
s_in[i] := rep_w;
end;
inc(i);
end;
result := s_in;
end;
in your case we just call function like this
ep_str('aa;bb', #59, #32);
and result will be 'aa bb'
regards,
Kristao
function rep_str(s_in: string; rep: char; rep_w: char): string;
var
i: integer;
go: boolean;
l: integer;
begin
i := 1;
result := '';
while i <> length(s_in) do
begin
if s_in[i] = rep then
begin
go := true;
if i <> 0 then
begin
if trystrtoint(s_in[i - 1], l) then go := false;
if i <> 1 - 2 then
begin
if trystrtoint(s_in[i - 2], l) then go := false;
end;
end;
if i <> length(s_in) then
begin
if trystrtoint(s_in[i + 1], l) then go := false;
if i <> length(s_in) - 1 then
begin
if trystrtoint(s_in[i + 2], l) then go := false;
end;
end;
if go then s_in[i] := rep_w;
end;
inc(i);
end;
result := s_in;
end;
this will be more user friendly code:
function rep_str(s_in: string; rep: char; rep_w: char): string;
var
i: integer;
go: boolean;
l: integer;
k: integer;
begin
i := 1; //inicalize
result := '';//inicalize
while i <> length(s_in) do //check every char in string
begin
if s_in[i] = rep then //if we foun unwanted char
begin
go := true; //inicalize
for k := 1 to 2 do //how many digits after you want to check now its 2
begin
if trystrtoint(s_in[i - k], l) then go := false; //n digits from unwanted char
if trystrtoint(s_in[i + k], l) then go := false; //n digits from unwanted char
if go = false then break;//break if found
end;
if go then s_in[i] := rep_w; //can we change unwanted char
end;
inc(i);
end;
result := s_in;
end;
regards,
Kristao
Function ReplaceChar(Str:string;ChO
var
i:integer;
Begin
result := '';
for i := 1 to length(Str) do
begin
if (Str[i] = ChOld) then
begin
if (i >= 2) and (i <= length(Str) ) then
begin
if (Str[i-1] in ['a'..'z','A'..'Z']) and (Str[i-2] in ['a'..'z','A'..'Z']) and (Str[i+1] in ['a'..'z','A'..'Z']) and (Str[i+2] in ['a'..'z','A'..'Z']) then
begin
Str[i] := ChNew;
end;
end
else
begin
// if the semicolon is the first or second
// element in your string
end;
end;
end;
result := Str;
end;
for example :
showmessage(ReplaceChar('1
Kristao, "for" is faster than "while", at least if compiled with Delphi 6 (I believe that's because "for" loop has less to do with memory than "while" has; I'm not sure about this because I'm not an ASM spec - can't analyse how Delphi works with "for" and "while/repeat" loops). Here's small test:
var
t : cardinal;
n : integer;
begin
t := gettickcount;
{} // avg 10.0 secs
for n := 0 to high(n) - 1 do ;
for n := 0 to high(n) - 1 do ;
for n := 0 to high(n) - 1 do ;
{}
{ // avg 12.7 secs
n := 0; while n < high(n) do inc(n);
n := 0; while n < high(n) do inc(n);
n := 0; while n < high(n) do inc(n);
{}
{ // avg 12.7 secs
n := 0; repeat inc(n) until n = high(n);
n := 0; repeat inc(n) until n = high(n);
n := 0; repeat inc(n) until n = high(n);
{}
caption := inttostr(gettickcount-t);
end;
Want to know what is slow? Using Delphi "string" - that's what is slow. And accessing single char in Delphi string also should be slow. And again - small test:
var
t : cardinal;
n : integer;
s : string;
p : pchar;
begin
t := gettickcount;
s := 'some strange string';
{} // avg 74.2 secs - fell asleep, aaargh
for n := 0 to high(n) - 1 do s[1] := 'S';
for n := 0 to high(n) - 1 do s[2] := 'O';
for n := 0 to high(n) - 1 do s[3] := 'M';
for n := 0 to high(n) - 1 do s[4] := 'E';
{}
{ // avg 16.9 secs
p := @s[1];
for n := 0 to high(n) - 1 do p[0] := 'S';
for n := 0 to high(n) - 1 do p[1] := 'O';
for n := 0 to high(n) - 1 do p[2] := 'M';
for n := 0 to high(n) - 1 do p[3] := 'E';
{}
caption := inttostr(gettickcount-t);
end;
for is slower than while and its true. I'm workt with server side applications and the most cycles was made on for. And performace on it was slower than when i remade it to while cycle , and that isn't 300 char long string what for cycles operated with there was 300 connections and lots of thouse dam strings :) . I took some time to study how for and while cycles look in ASM and there is a reason why for could be slower that while. FOR its self is loop and i we look in CPU what kind of operations are made there than there is an answer why.
ok test is good but in this kind of test you won't feelable results. Its better from beginig to train your self to use while cycles.
I showed an example where FOR is faster than WHILE. It means that there ARE situations when FOR loops are faster.
Another thing - why do you use Delphi STRING, if it's *very* slow?
Another thing - "why to use slower cycles? :)" <-- well, you should start using plain ASM in coding (with no pascal code, with no delphi objects etc)
Business Accounts
Answer for Membership
by: heretoreadPosted on 2006-04-25 at 09:16:57ID: 16535617
StringReplace();
Look in delphi help for details.