Link to home
Start Free TrialLog in
Avatar of dingcheng
dingcheng

asked on

How to seperate a string?

I have a string like "abcdef,student,expert,".
How can i seperate them by",";
I want to get three string. The result is "abcdef" "student"
"expert"
Avatar of rarigo
rarigo

Hi,

 You could use this code:

function GetToken( TokenString : String;  TokenPos : Integer;  Separator : Char ) :  String;
Var
   x, i, j : Integer;
begin
   x := 1;
   i := 0;
   while ( i < TokenPos ) do
   begin
     j := x;
     while ( TokenString[x] <> Separator ) and
           ( x <= Length( TokenString )  ) do   Inc( x );
     Inc( i );
     Inc( x );
   end;
   Result := Copy(TokenString, j, x-j-1);
end;



begin
   Str := GetToken(' abcdef,student,expert',Pos, ',' );
  // Where pos vary from 0 to numbers of substrings - 1 in the string'
end;


Sinceramente,
Reginaldo


Var s1:string[255] = "abcdef,student,expert";
s2:array[1..100] of string[255];
i,j,k:integer;


k = 1;
j = 1;
while i < length(s1) do
Begin
     while i <> "," do
     begin
          s2[k][j] := s1[i];
          j++;
          i++;
     end;
     k++;
     j=1;
     i++;
end;

s2 holds your separated strings
s2[1] = "abcdef"
ASKER CERTIFIED SOLUTION
Avatar of Motaz
Motaz

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
Use the CommaText function of TStringList.
U can assign
var
 strList : TstringList;
  .......
  ....

  StrList.CommaText  := "abcdef,student,expert";
This will cut the strings ,with  comma as the delimiter.