Simple Split Function - Reading settings from executables.

posted 8 Feb 2010, 03:50 by Delphi Basics   [ updated 8 Feb 2010, 03:58 ]
After reading our previous tutorials, many of you will be using resources to store the settings for your applications.
To avoid writing and reading settings separately, you could write your settings:
 
Good, Morning and World
as
"Good||Morning||World||"  [The delimiter is "||" in this case]
 
This split function parses your settings after reading from resources into a string array for use in your application.

Coder: steve10120

Function
TStringArray = array of string;

function Split(Expression:string; Delimiter:string):TStringArray;
var
  Res:        TStringArray;
  ResCount:   DWORD;
  dLength:    DWORD;
  StartIndex: DWORD;
  sTemp:      string;
begin
  dLength := Length(Expression);
  StartIndex := 1;
  ResCount := 0;
  repeat
    sTemp := Copy(Expression, StartIndex, Pos(Delimiter, Copy(Expression, StartIndex, Length(Expression))) - 1);
    SetLength(Res, Length(Res) + 1);
    SetLength(Res[ResCount], Length(sTemp));
    Res[ResCount] := sTemp;
    StartIndex := StartIndex + Length(sTemp) + Length(Delimiter);
    ResCount := ResCount + 1;
  until StartIndex > dLength;
  Result := Res;
end;

Usage:
var
  Strings:  TStringArray;

begin
  Strings := Split('Good||Morning||World||', '||');
  MessageBox(0, PChar(Strings[2]), nil, 0);

end.
Comments