Replace a character???

is there a function to replace a character rather doing it by searching each character. Example, when someone types D'Amico , I want that everytime theres a ' in a word, it must add another ' so that it will be D''Amico. Anyone know how without doing it manually meaning look for each letter to see if its a '. Thanks
fb132Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lukasz LachCommented:
s := StringReplace(s, 'old_char', 'new_char', [rfReplaceAll]);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
esoftbgCommented:
procedure ReplaceChar(S: string; new_char,old_char: Char);
var
  I:  Integer;
begin
  while (Pos(old_char, S)>0) do
  begin
    I := Pos(old_char, S);
    S[I] := new_char;
  end;
end;
0
Slick812Commented:
hello fb132, I am not understanding what you want to do, but you say  "when someone types D'Amico". . . so I guess this is in a TMemo or Tedit ect?. .  anyway, there is a OnKeyPress event in Text input controls, which let's you monitor the charaters, that go into the control, you can change or add charaters in the KeyPress

procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);
begin
inherited;
if Key = ''' then
 Memo1.SelText := Key;

end;
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Slick812Commented:
should be
if Key = '''' then
0
esoftbgCommented:
i forgot it must to be function:

function ReplaceChar(S: string; new_char,old_char: Char): string;
var
 I:  Integer;
begin
  try
    while (Pos(old_char, S)>0) do
    begin
      I := Pos(old_char, S);
      S[I] := new_char;
    end;
  finally
    Result := S;
  end;
end;
0
Lukasz LachCommented:
s := StringReplace(s, '''', '''''', [rfReplaceAll]);

That changes ' to '', what is the problem? ;-)
0
Wax0rCommented:
Also Slick, my guess is it's to make sure SQL server or an Access database or god knows what else doesn't bitch about storing things when there's only one ' in there.

anAKIN answered it though
0
Lukasz LachCommented:
why c? isn't that the best way to replace chars so what you expected? ;/
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Fonts Typography

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.