Solved

how can i split this string?

Posted on 2009-05-14
6
1,104 Views
Last Modified: 2012-05-06
im new to delphi...
 i have a string like this  " 123|asd1|fhgj2|er3|45678| " 
and i need to split it up.
i figured out how to do it IF it has different separators (eg " %123|asdf*fhgj^er|) like this
textin := %123|asdf|fhgj|er|45678|
 text1 := copy(textin, 1, pos('%', textin)-1);
  Delete(textin, 1, pos('%', textin)-1);
 text2 := copy(textin,1 ,pos('|', textin)-1);
  Delete(textin, 1, pos('|', textin)-1);
...
  L := Form2.ListView1.Items.Add;
  L.Caption := info;
  L.SubItems.Add(text1);
  L.SubItems.Add(text2);
ect

but how do i do it if the separators are the same?


0
Comment
Question by:electrodude102
  • 3
  • 2
6 Comments
 
LVL 2

Accepted Solution

by:
Usurpatirus earned 50 total points
ID: 24382522
you may use StringReplace function, or while loop:
procedure TForm1.Button2Click(Sender: TObject);

var

Str : String;

begin

Str := '%123|asdf*f|hgj^|er|';
 

   with Listview1.Items.Add do

   begin

        Caption := Copy(Str,1, Pos('|',Str)-1);

        system.Delete(Str,1,Pos('|',Str));

   while Trim(Str)<>'' do

   begin

        Subitems.Add(Copy(Str,1, Pos('|',Str)-1));

        System.Delete(Str,1,Pos('|',Str));

   end;

   end;

end;
 
 

procedure TForm1.Button1Click(Sender: TObject);

var

Str : String;

begin

Str := '%123|asdf*f|hgj^|er|';

with Listview1.Items.Add do

begin

        Caption := Copy(Str,1, Pos('|',Str)-1);

        System.Delete(Str,1,Pos('|',Str));

        SubItems.Text := (StringReplace(Str,'|',#13,[rfReplaceAll, rfIgnoreCase]));

end;

end;

Open in new window

0
 
LVL 9

Expert Comment

by:Graceful_Penguin
ID: 24382549
You just have to loop the code you have there and it will work. I used ShowMessage to test if it works but I have also given you the code to add it to the listview in comments.
textin := '123|asdf|fhgj|er|45678';

while (pos('|', textin) > 0) do

begin

  text1 := copy(textin, 0, pos('|', textin)-1);

  Delete(textin, 1, pos('|', textin));
 
 

//  L := Form2.ListView1.Items.Add;

//  L.Caption := info;

//  L.SubItems.Add(text1);

ShowMessage(text1);

ShowMessage(textin);

Open in new window

0
 
LVL 9

Expert Comment

by:Graceful_Penguin
ID: 24382556
sorry did not copy the end;
textin := '123|asdf|fhgj|er|45678';

while (pos('|', textin) > 0) do

begin

  text1 := copy(textin, 0, pos('|', textin)-1);

  Delete(textin, 1, pos('|', textin));

 

 

//  L := Form2.ListView1.Items.Add;

//  L.Caption := info;

//  L.SubItems.Add(text1);

  ShowMessage(text1);

  ShowMessage(textin);

end;

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:electrodude102
ID: 24382733
Graceful_Penguin: that works but i need a var(?) for each "set" of the string (not sure how to explain it..)
 
example: if the string in was '123|asdf|fhgjer|45678' it does this
text0 := 123
text1 := asdf
text2 := fhgjer
text3 := 45678

(cause i need to use them later, get it?)
0
 
LVL 9

Expert Comment

by:Graceful_Penguin
ID: 24382874
The easiest way is to use a stringlist to store the strings but if you don't like that you can use a dynamic array. Here is the source for the string list. Just remember to destroy it when you are done.
var textin, text1 : string;

 Strings : TStringlist;

 counter : integer;

begin

   textin := '123|asdf|fhgj|er|45678';

   Strings := TStringlist.Create;

   counter := 0;

while (pos('|', textin) > 0) do

begin

  Strings.Add(copy(textin, 0, pos('|', textin)-1));

  Delete(textin, 1, pos('|', textin));
 
 

//  L := Form2.ListView1.Items.Add;

//  L.Caption := info;

//  L.SubItems.Add(text1);

   ShowMessage(Strings[counter]);

   ShowMessage(textin);

   inc(counter);

Open in new window

0
 
LVL 1

Author Comment

by:electrodude102
ID: 24387267
im going to accept Usurpatirus answer cause after messing around with it it seems he most usable, thanks you all your help
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi 2007 printer setup problem 8 755
Conversion ? 15 88
Object was open using ADO and Delphi 2007 33 195
Mydac connection data base issue 3 139
We need a new way to communicate time sensitive or critical info.   The best part of my role at xMatters is visiting our clients all over the world to learn about how they operate their businesses, share insights that xMatters has gleaned across…
An analysis of the phishing scam that has been affecting Google users, along with steps to take for protection, as well as what to do if you receive one of the emails.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

18 Experts available now in Live!

Get 1:1 Help Now