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

x
?
Solved

extracting data from # delimited string

Posted on 2005-03-14
7
Medium Priority
?
396 Views
Last Modified: 2010-04-05
hello,

i have a string which varies in length containing numbers which are # delimited e.g

123#444444#432#888#333454544#878787#3333

i'm trying to use the pos function but getting a little lost.  how can i extract each number one by one in order to deal with it?

i.e. extract 123 and then do, extract 444444 then do etc

cheers
0
Comment
Question by:Oli999
  • 3
  • 2
  • 2
7 Comments
 
LVL 27

Assisted Solution

by:kretzschmar
kretzschmar earned 400 total points
ID: 13534295
i saw you invent the wheel more often as needed

a simple generic function, will all do for you

function splitstr(var astring : String; Delimiter : String) : String;
var
  p : Integer;
begin
  result := '';
  if AString <> '' then
  begin
    p := pos(Delimiter,AString);
    if p > 0 then
    begin
      result := copy(AString,1,p-1);
      AString := copy(AString,p+length(Delimiter),maxLongInt);
    end
    else
    begin
      result := AString;
      AString := '';
    end;
  end;
end;

usage

var
  s : string;
  sl : Tstringlist;
begin
  s := '123#444444#432#888#333454544#878787#3333';
  sl := TStringList.Create;
  while s <> '' do sl.add(splitstr(s,'#'));
  memo1.lines.assign(sl);
  sl.free;
end;

meikl ;-)

0
 
LVL 6

Accepted Solution

by:
rbohac earned 100 total points
ID: 13534407
Another Way:

Place a TMemo and a TButton on a form...

procedure TForm1.Button1Click(Sender: TObject);
var
  s : String;
begin
  s := '123#444444#432#888#333454544#878787#3333';
  Memo1.Lines.Delimiter     := '#';
  Memo1.Lines.DelimitedText := s;
end;
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 13534498
>Another Way:
since d6 or 7, not for prior versions
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 46

Expert Comment

by:aikimark
ID: 13534745
How about using the built-in parsing functions of a TStringList?!?
http://www.seanet.com/~pgm/delphi/parsetolist.htm
http://www.delphibasics.co.uk/RTL.asp?Name=TStringList

* How about using the TParser class?
http://www.delphipages.com/tips/copyview.cfm?ID=123=
0
 
LVL 6

Expert Comment

by:rbohac
ID: 13534917
Actually the comment I left basically does that.. Both inherit from TStrings
0
 
LVL 46

Expert Comment

by:aikimark
ID: 13535219
rbohac,

Thanks.  I didn't know that TMemo inherited from TString.

Are there any performance difference between using a TString object and a TMemo object?
0
 
LVL 6

Expert Comment

by:rbohac
ID: 13535271
Yes absolutely, simply because a TMemo is drawn to the screen. I used a Tmemo because its easy to visually see the string being split. You could just as easily do:


procedure TForm1.Button1Click(Sender: TObject);
var
  s : String;
  StringList : TStringList;
begin
  s := '123#444444#432#888#333454544#878787#3333';
  StringList := TStringList.create;
  try
    StringList.Delimiter     := '#';
    StringList.DelimitedText := s;
    ShowMessage(IntToStr(StringList.Count));
    //Do something with the data
  finally
    StringList.Free;
  end;
end;

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

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…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

578 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