extracting data from # delimited string

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
LVL 2
Oli999Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kretzschmarCommented:
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
rbohacCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kretzschmarCommented:
>Another Way:
since d6 or 7, not for prior versions
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

aikimarkCommented:
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
rbohacCommented:
Actually the comment I left basically does that.. Both inherit from TStrings
0
aikimarkCommented:
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
rbohacCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.