Mega Code Archive

 
Categories / Delphi / Strings
 

Function to split string into array

Title: Function to split string into array function stringtoarray(value,delimiter : string;):tstrings; var dx : integer; ns : string; txt : string; sl : TStrings; delta : integer; begin sl:=TStringList.create; 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) ; sl.Add(ns) ; txt := Copy(txt,dx+delta,MaxInt) ; end; finally sl.EndUpdate; end; stringtoarray:=sl; sl.free; end; An example on how to use this function: var list:tstrings; begin list:=tstringlist.create; list:=stringtoarray('delphi;zone;web;site',';'); list.free; end;