Solved

Function to Replace a value in a String

Posted on 2001-06-20
5
142 Views
Last Modified: 2010-04-06
Hi,

I'd like to know if there is a function which will replace
a value by another in a string.

I have a string which containts ' . I need to double the '
So I'd like to replace ' by '' .

Example: 'You'll find'   must be 'You''ll find'


Thank you !

Mary
0
Comment
Question by:msl22
5 Comments
 
LVL 3

Accepted Solution

by:
nnbbb09 earned 20 total points
ID: 6212010

You can use StringReplace

eg : result:=StringReplace('Some Text','Text','Message',[rfReplaceAll])

This will return 'Some Message' as the result.

There is also a function called QuotedStr which is used specifically for replace single quotes ' with double quotes ''.

regards

Jo
0
 
LVL 1

Expert Comment

by:Dennis9
ID: 6212027
Hi.
nnbbb09 is right, but u could also use this:

procedure TForm1.Button1Click(Sender: TObject);
var
s: String;
FoundPos, i: Integer;
begin
s := Edit1.Text;
for i := 1 to Length(s) do
begin
FoundPos := Pos(' ', s);//The stuff to search for
if FoundPos <> 0 then
begin
Delete(s, FoundPos, 1);
Insert('+', s, FoundPos);//The item to replace with
end;
end;
Edit1.Text := s;
end;


//Another exampel of StringReplace:
edit1.text := StringReplace(Edit1.Text, ' ', '+', [rfReplaceAll]);


Hope it helped
Dennis
0
 
LVL 3

Expert Comment

by:VSF
ID: 6212152
This should do it!
Just another way....

VSF
www.enge.cjb.net
www.victory.hpg.com.br

function ReplaceText(const S,ReplacePiece,ReplaceWith: String):String;

Var Position: Integer;
    TempStr: String;
begin
  Position := Pos(ReplacePiece,S);
  if Position > 0 then Begin
    TempStr := S;
    Delete(TempStr,1,Position-1+Length(ReplacePiece));
    Result :=
Copy(S,1,Position-1)+ReplaceWith+ReplaceText(TempStr,ReplacePiece,ReplaceWith)

  End else Result := S;
end;
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6213122
If you need something as fast as possible, you should look here for a solution that will outperform all other suggestions, that were made yet...   :-)

http://help.madshi.net/Data/StringManipulate.htm#ReplaceStr

Regards, Madshi.
0
 

Author Comment

by:msl22
ID: 6214119
Thank you for all of you !!!
I really appreciate your comment !

Have a nice day.

Mary
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

773 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