Link to home
Start Free TrialLog in
Avatar of eNarc
eNarcFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Parsing String

Hi, I've got this function though its not what I'm wanting though it works great.

I like it to be something like

var
  sl:tstringlist;
....


function Parse(String,Delimiter:string):string;
begin
  ...

  result:=sl.text;
end;


is there a better way than the in the code. because I want to be able to do stuff with it

Parse('dsfdsfsdfdssd/ff/sd/fsd/f/sd/fsd/f/sd/sf/s/df/sdf/sd/','/');

richedit1.lines.add(sl[3]);

Procedure ParseLastPart(const sl : TStrings; const value : string; const delimiter : string);
var
   dx : integer;
   ns,o : string;
   txt : string;
   delta : integer;
begin
   delta := Length(delimiter) ;
   txt := value + delimiter;
   sl.BeginUpdate;
   sl.Clear;
   try
     while Length(txt) > 0 do
     begin
       dx := Pos(delimiter, txt) ;
       ns := Copy(txt,0,dx-1) ;
       o:=ns;
       sl.Add(ns) ;
       txt := Copy(txt,dx+delta,MaxInt) ;
     end;
   finally
     form1.RichEdit2.lines.add(o);
     sl.EndUpdate;
   end;
end;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ThievingSix
ThievingSix
Flag of United States of America image

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
I suggest you to use ExtractStrings function like this the following code:

var sl: TStringList;
begin
sl := TStringList.Create;
  try
  ExtractStrings(['/'], [' '], 'dsfdsfsdfdssd/ff/sd/fsd/f/sd/fsd/f/sd/sf/s/df/sdf/sd/', sl);
  richedit1.lines.add(sl[3]);
  finally
  sl.Free;
  end;
end;

Open in new window

Your function will be


function Parse(Text:string; Index: integer; Delimiter: char): string;
var sl: TStringList;
begin
sl := TStringList.Create;
  try
  sl.Text := Text;
  ExtractStrings([Delimiter], [' '], Pchar(Text), sl);
  Result := sl[Index];
  finally
  sl.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
richedit1.lines.add(Parse('dsfdsfsdfdssd/ff/sd/fsd/f/sd/fsd/f/sd/sf/s/df/sdf/sd/', 3, '/'));
end;

Open in new window

Avatar of eNarc

ASKER

When I use StringList.Delimiter:= '/'; it also splits the spaces too? "5/5444 stfgvg /ughggg //56666 fghhh gggg/"
That's why you need to set StrictDelimeter := True.
Avatar of eNarc

ASKER

i can't seem accept the 2 comments.
Avatar of Mike McCracken
Mike McCracken

Click the ACCEPT MULTIPLE SOULTIONS link and follow the instructions

mlmcc