Solved

split a delimited string into its sub-strings

Posted on 1999-01-02
11
247 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
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Wininet read php file from internet issue 6 91
Printing problem 2 92
Delphi Form ownership 4 88
Moving (cutting/pasting) controls in a TTabbedNotebook... 7 34
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…
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
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…

770 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