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"
dingchengAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rarigoCommented:
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


0
bcolemanCommented:
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"
0
MotazCommented:
Write a function like this:

procedure GetToken(DataLine: string; sep: char; var x: TStringList);
var
  i: Integer;
begin
  i:= 0;
  if DataLine[Length(DataLine)] <> Sep then
    DataLine:= DataLine + Sep;
  repeat
    x.Add(Copy(DataLine, 1, Pos(Sep, DataLine) - 1));
    Delete(DataLine, 1, Pos(Sep, DataLine) );
  until DataLine = '';

end;


drop a button and a memo, double click on the button and write:

procedure TForm1.Button1Click(Sender: TObject);
var
  List: TStringList;
begin
  List:= TStringList.Create;
  GetToken('First,Second,Third,Last', ',', List);
  Memo1.Lines:= List;

end;

Motaz
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rajeshveeCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.