Solved

split a delimited string into its sub-strings

Posted on 1999-01-02
11
251 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
Technology Partners: 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

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 I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 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