Link to home
Start Free TrialLog in
Avatar of t0m3k
t0m3k

asked on

Text Strings

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...)
ASKER CERTIFIED SOLUTION
Avatar of vperez
vperez

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mvz121697
mvz121697

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;

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);
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