?
Solved

split a delimited string into its sub-strings

Posted on 1999-01-02
11
Medium Priority
?
253 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 80 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
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
Course of the Month12 days, 21 hours left to enroll

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