Solved

Text Strings

Posted on 1998-10-23
4
139 Views
Last Modified: 2010-04-06
How do I do if I want to split a string in, lets say, three parts.
Each of the parts is separated by a semicolon, and the words should be able to have different lenghts.
one;two;three; or word1;blaha;hello
Each of the words should be put in a seperate string (like string1, string2, string3, or whatever...)
0
Comment
Question by:t0m3k
[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
  • 2
4 Comments
 

Accepted Solution

by:
vperez earned 100 total points
ID: 1344156
Ok...use the Pos function to find the semicolons like so it will give you where the first semicolon is.  Then use Copy and Delete. Like so:

end := Pos(";",mainstring);
string1 := Copy(mainstring,0,end-1);
mainstring := Delete(mainstring,0,end-1);

end := Pos(";",mainstring);
string2 := Copy(mainstring,0,end-1);
mainstring := Delete(mainstring,0,end);

Do this as many times as it takes...
0
 
LVL 2

Expert Comment

by:mvz121697
ID: 1344157
var cTotal,cSub1,cSub2,cSub3:string;
var nPos:integer;
begin
  cTotal:='one;two;three';
  cSub1:=cTotal;
  cSub2:='?';
  cSub3:='?';
  nPos:=Pos(';',cTotal)    ;
  if nPos>0 then begin
    cSub1:=copy(cTotal,1,nPos-1);
    cTotal:=Copy(cTotal,nPos+1,255);
    cSub2:=cTotal;
    nPos:=Pos(';',cSub2);
    if nPos>0 then begin
       cSub2:=copy(cTotal,1,nPos-1);
       cSub3:=Copy(cTotal,nPos+1,255);
    end;
  end;
  ShowMessage(cSub1);
  ShowMessage(cSub2);
  ShowMessage(cSub3);
end;

0
 

Expert Comment

by:vperez
ID: 1344158
well no offense or anything but mine is simpler...
and you can put it in a loop like so

i := 1;
Do
     i := i + 1;
     end := Pos(";",mainstring);
     string[i] := Copy(mainstring,0,end-1);
     mainstring := Delete(mainstring,0,end-1);
while(Pos(";",mainstring) = 0);
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1344159
Here is how it is done....

Function Parse(TheItemStr, ParseStr : String; ItemNum : Integer): String;
 Var
  I4 : Integer;
  S4 : String;
Begin
TheItemStr := TheItemStr + ParseStr;
  S4 := '';
 I4 := 0;
 While I4 <= ItemNum Do
  Begin
   S4 := Copy(TheItemStr, 1, Pos(ParseStr, TheItemStr) - 1);
   Delete(TheItemStr, 1, Pos(ParseStr, TheItemStr));
   Inc(I4);
  End;
  Result := S4;
End;
Example call:
MyString := Parse(Memo1.Text, ';', 1);

Cheers,
Viktor
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

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