Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Replace a character???

Posted on 2003-03-07
8
Medium Priority
?
235 Views
Last Modified: 2013-12-03
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
0
Comment
Question by:fb132
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 5

Accepted Solution

by:
Lukasz Lach earned 40 total points
ID: 8089964
s := StringReplace(s, 'old_char', 'new_char', [rfReplaceAll]);
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 8091570
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
 
LVL 34

Expert Comment

by:Slick812
ID: 8092713
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 34

Expert Comment

by:Slick812
ID: 8092719
should be
if Key = '''' then
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 8092777
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
 
LVL 5

Expert Comment

by:Lukasz Lach
ID: 8093359
s := StringReplace(s, '''', '''''', [rfReplaceAll]);

That changes ' to '', what is the problem? ;-)
0
 
LVL 1

Expert Comment

by:Wax0r
ID: 8093531
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
 
LVL 5

Expert Comment

by:Lukasz Lach
ID: 8093791
why c? isn't that the best way to replace chars so what you expected? ;/
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question