Solved

split a delimited string into its sub-strings

Posted on 1999-01-02
11
250 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
[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
  • 5
  • 3
  • 3
11 Comments
 
LVL 20

Accepted Solution

by:
Madshi earned 20 total points
ID: 1354201
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
ID: 1354202
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
ID: 1354203
Ooops,

please replace "copyR(str,c1)" with "copy(str,c1,maxInt)"
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!

 

Author Comment

by:mathes
ID: 1354204
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
ID: 1354205
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
 

Author Comment

by:mathes
ID: 1354206
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
ID: 1354207
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
ID: 1354208
Yes, meikl, that's right...  :-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1354209
hi Madshi,

how you said in your last comment
0
 

Author Comment

by:mathes
ID: 1354210
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
ID: 1354211
No problem...  :-)
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

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…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

749 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