Solved

Function to Replace a value in a String

Posted on 2001-06-20
5
140 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now