Solved

How to do this?

Posted on 1998-09-10
12
230 Views
Last Modified: 2010-04-06
i got a string...add1~add2~add3~add4
i want to replace ~ with return
what is the command??
If i have a string such as AA3123, i want to convert to 3123..What function should i use..Is there any free componets  available for download. Where can i find free componets widely available?
Anyone?? Thanx...
0
Comment
Question by:dagger
  • 5
  • 3
  • 2
  • +2
12 Comments
 

Author Comment

by:dagger
ID: 1339211
Edited text of question
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1339212
dagger,

(1) i1:=Pos('~',str);
    while i1<>0 do begin
      Delete(str,i1,1); Insert(#$D#$A,str,i1);
      i1:=Pos('~',str);
    end;
(2) Delete(str,1,2);

Is that what you need?

Regards, Madshi.
0
 
LVL 4

Expert Comment

by:d003303
ID: 1339213
Yo dagger,
your starting point for free components can be
the DSP (Delphi Super Pages)
http://sunsite.icm.edu.pl/delphi/

The Delphi Exchange
http://www.delphiexchange.com/

or Torry's Delphi pages
http://carbohyd.siobc.ras.ru/torry/

Replacing characters in strings is quite easy. Take a look at the functions Insert, Delete, Copy and Pos.

slash/d003303
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:dagger
ID: 1339214
Thank you guys...
The reason i am asking, is b'cos i find it very difficult to do programming if i were to replace ~ with Return. If i can find a suitable component or maybe a function which enable me to do this i.e
Str1 = address1~address2~address3~address4
NewStr = Replace("~","Return",Str1)
So i get the new formatted address...

but anyway i will check the other functions...Thank you Madshi and d003303. Really appreciate.
0
 
LVL 10

Accepted Solution

by:
Jacco earned 0 total points
ID: 1339215
Here is a function I use:

function StrTran( sSource, sSearch, sReplace: string ): string;
var
  iPosition: integer;
begin
  iPosition := Pos( sSearch, sSource );
  while iPosition > 0 do begin
    sSource   := copy( sSource, 1, iPosition-1 ) + sReplace + copy( sSource, iPosition+length(sSearch), length(sSource)-iPosition );
    iPosition := Pos( sSearch, sSource );
  end;
  Result := sSource;
end;

Works for me!!

Regards Jacco
0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1339216
function Replace(s: string; sfrom, sto: string): string;
begin
  while Pos(sfrom, s) > 0 do begin
    s := Copy(s, 1, Pos(sfrom, s) - 1) + sto +
         Copy(s, Pos(sfrom, s) + Length(sfrom),
           Length(s) - Pos(sfrom, s) + Length(sfrom) - 1);
  end;
  Result := s;
end;

// replaces sfrom in s with sto

looks awful, can be done much better, but is understandable.

Black Death.
0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1339217
OH MY GOD!

jacco - extremely sorry for this!!!
hit me!!!
i don't know how i could have missed your answer.
pleez don't be angry at me, i'm afraid i've gone blind...
or am i already dead?

mercy - merci.

have a nice day y'all.
that's it for me. i'm gonna go back to sleep...

so long,

Black Death.

0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1339218
whoops:
you should add

if sfrom=sto then
  Exit;

as first statement of the proc,
otherwise you would waste *_infinite_* runtime...

Black Death.
0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1339219
ohman:

if sfrom = sto then begin
  Result := s;
  Exit;
end;

Black Death.
0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1339220
that's for your's too, jacco...
0
 
LVL 10

Expert Comment

by:Jacco
ID: 1339221
BlackDeath:

You're right!

I'll add it in my library source!

Regards Jacco
0
 
LVL 10

Expert Comment

by:Jacco
ID: 1339222
The function then becomes:

function StrTran(sSource, sSearch, sReplace: string ): string;
var
  iPosition: integer;
begin
  if sReplace<>sSearch then begin
    iPosition := Pos( sSearch, sSource );
    while iPosition > 0 do begin
      sSource   := copy( sSource, 1, iPosition-1 ) + sReplace + copy( sSource, iPosition+length(sSearch), length(sSource)-iPosition );
      iPosition := Pos( sSearch, sSource );
    end;
  end;
  Result := sSource;
end;

I try to prevent using exit to much
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Simple Delphi Question 9 96
Delphi: how to implement a User Shortcut mapper? 1 121
Delphi - replicating a form 8 82
Create a path if not exists 7 92
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

830 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