[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 186
  • Last Modified:

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"
0
dingcheng
Asked:
dingcheng
1 Solution
 
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
 
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

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now