?
Solved

extracting data from # delimited string

Posted on 2005-03-14
7
Medium Priority
?
393 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

777 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