Parsing String

eNarc
eNarc used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Have you tried:

StringList.StrictDelimiter := True;
StringList.Delimiter:= '/';
StringList.DelimitedText := 'dsfdsfsdfdssd/ff/sd/fsd/f/sd/fsd/f/sd/sf/s/df/sdf/sd/';
ShowMessage(StringList.Strings[3]);

?

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

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

Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Author

Commented:
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.

Author

Commented:
i can't seem accept the 2 comments.
Mike McCrackenSenior Consultant
Most Valuable Expert 2011
Top Expert 2013

Commented:
Click the ACCEPT MULTIPLE SOULTIONS link and follow the instructions

mlmcc

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial