Solved

split a delimited string into its sub-strings

Posted on 1999-01-02
11
245 Views
Last Modified: 2010-04-04
Hello experts,

I want to split the delimited masterstring

masterstring := '|aaa|bbb|ccc|ddd|';

to its sub-strings: 'aaa' bbb' 'ccc'

In order to accomplish this, I wrote a little demo program:

procedure TForm1.Button1Click(Sender: TObject);
var
  s, masterstring, resultstring: string;
  sl: tstringlist;
  i:integer;
begin
  masterstring := '|aaa|bbb|ccc|ddd|';
  sl := tstringlist.create;
  while pos('|', masterstring) > 0 do
  begin
    s := copy(masterstring, 1, pos('|', masterstring));
    delete(s, length(s), 1);
    sl.add(s);
    delete(masterstring, 1, length(s) + 1);
  end;
  sl.add(s);
  // now all substrings are in a stringlist

  for i := 0 to sl.count - 1 do
    resultstring := resultstring + sl.strings[i];
  edit1.text := resultstring;
  sl.free;

This code (almost!) works as desired.

My problem is:

The masterstring := '|aaa|bbb|ccc|ddd|';

is  convertet to

resultstring := 'aaabbbcccdddddd';

So the last sub-string  (ddd)
 
unfortunately is contained twice i the resultstring.

Of course my desired resultstring must have this shape:

resultstring := 'aaabbbcccddd';

Can you please help me and tell me what is wrong here?

With kind regards

Mathes

0
Comment
Question by:mathes
  • 5
  • 3
  • 3
11 Comments
 
LVL 20

Accepted Solution

by:
Madshi earned 20 total points
Comment Utility
Hi Mathes,

if you are sure that there's a "|" at the end of the masterstring then the
  sl.add(s);
after then "end" of the while loop should be deleted.

However, perhaps you want to use my functions. They're much faster, but the master string must have the form "aaa|bbb|ccc".
A master string of the form "|aaa|bbb|ccc|" would produce 5 sub-strings ('','aaa','bbb','ccc','').

function SubStrCount(str: string) : cardinal;
var c1 : cardinal;
begin
  result:=0;
  if str='' then exit;
  c1:=0; repeat inc(result); c1:=findStr('|',str,c1+1,maxCard) until c1=0;
end;

function RetSubStr(str: string; index: cardinal) : string;
var c1,c2 : cardinal;
begin
  result:='';
  if (str='') or (index<1) then exit;
  c2:=0;
  repeat
    dec(index);
    c1:=c2+1; c2:=findStr('|',str,c1,maxCard);
  until index=0;
  if c2=0 then result:=copyR(str,c1) else result:=copy(str,c1,c2-c1);
end;

Example:

i2:=SubStrCount(master);
for i1:=1 to i2 do
  sl.add(RetSubStr(master));

Regards, Madshi.
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
hi mathes,

why splitting, if your resultstring contains all except delimteres, you can delete the delimters, like a simple function like this:
function remove_delimeter(RS,DS : String) : String;
var s : String;
begin
  s := RS;
  while pos(DS,S) > 0 do delete(s,pos(DS,S),Length(DS));
  result := s;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  memo1.text := remove_delimeter(memo1.text,edit1.text);
end;

meikl
0
 
LVL 20

Expert Comment

by:Madshi
Comment Utility
Ooops,

please replace "copyR(str,c1)" with "copy(str,c1,maxInt)"
0
 

Author Comment

by:mathes
Comment Utility
Hi experts,

thank you for your comments so far. If I delete the sl.add(s); after the "end" of the while loop
it happens the following:

The

 masterstring:= '|aaa|bbb|ccc|ddd|'

is converted to the


 resultstring:='ddd';

So now unfortunately only the last substring is contained in the resultstring,
the other sub-strings obviously dissapeared.

What is wrong here?


With kind regards

Mathes

P.S.: The function remove_delimeter and SubStrCount RetSubStr will be very useful in many other situations,
but in this specific case it must be done in the way as I tried it in my source.
0
 
LVL 20

Expert Comment

by:Madshi
Comment Utility
Hmmm, Mathes... You must have removed the wrong sl.Add(s).
Please remove the sl.Add(s) AFTER the end of the while loop!!!

I tried it...   :-)

Regards, Madshi.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:mathes
Comment Utility
Hi experts,

thank you for your comments so far. If I delete the sl.add(s); after the "end" of the while loop
it happens the following:

The

 masterstring:= '|aaa|bbb|ccc|ddd|'

is converted to the


 resultstring:='ddd';

So now unfortunately only the last substring is contained in the resultstring,
the other sub-strings obviously dissapeared.

What is wrong here?


With kind regards

Mathes

P.S.: The function remove_delimeter and SubStrCount RetSubStr will be very useful in many other situations,
but in this specific case it must be done in the way as I tried it in my source.
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
hi mathes,

in addition to your last comment, this reproduce what you want.

procedure TForm1.Button1Click(Sender: TObject);
var
  s, masterstring, resultstring: string;
  sl: tstringlist;
  i:integer;
begin
  masterstring := '|aaa|bbb|ccc|ddd|';
  sl := tstringlist.create;
  while pos('|', masterstring) > 0 do
  begin
    s := copy(masterstring, 1, pos('|', masterstring));
    delete(s, length(s), 1);
    sl.add(s);
    delete(masterstring, 1, length(s) + 1);
  end;
{sl.add(s);}
// now all substrings are in a stringlist
  for i := 0 to sl.count - 1 do
    resultstring := resultstring + sl.strings[i];
  edit1.text := resultstring;
  sl.free;
end;

meikl
0
 
LVL 20

Expert Comment

by:Madshi
Comment Utility
Yes, meikl, that's right...  :-)
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
hi Madshi,

how you said in your last comment
0
 

Author Comment

by:mathes
Comment Utility
Hi experts, thank you cor your advice. I want to excuse myself for having deleted the wrong statement. Actually your advice was clear enough


With kind regards

Mathes
0
 
LVL 20

Expert Comment

by:Madshi
Comment Utility
No problem...  :-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

15 Experts available now in Live!

Get 1:1 Help Now