Examples Delphi

I THINK THIS ROUTINE PROBABLY NEEDS
LOOKING AT: NOT 100% SURE IF IT WORKS
PROPERLY ALL OF THE TIME...
Hi
Here is a generic 'SplitStringByDelimiter' routine
which might be useful (if only for filing away)
Richard
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function SplitStringByDelimiter(longString: String; var subStrings: TStringList;
delimiter: Char): Integer;
///////////////////////////////////////////////////////////////////////////
procedure TMainForm.FormCreate(Sender: TObject);
var
lines: TStringList;
ballOfString: String;
returnValue: Integer;
tempString: String;
begin
lines := TStringList.Create;
ballOfString := 'this is a ball of string, useful for finding your way, in a dark cave, if you had the foresight to trail it, behind you on the way in';
returnValue := SplitStringByDelimiter(ballOfString, lines, ',');
if (returnValue = -1) then
begin
tempString := 'Problems were encountered when trying to split long strings';
Application.MessageBox(PChar(tempString), ' String Handling Error', mb_OK);
end;
lines.Free;
end;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function TMainForm.SplitStringByDelimiter(longString: String; var subStrings: TStringList;
delimiter: Char): Integer;
//split the string passed in in 'longString' into chunks that are (hopefully) separated
//by the character held in 'delimiter', and put the chunks into consecutive positions
//in the 'subStrings' string list (var parameter). An integer 'result' is also passed
//back to signify success or failure (1 for sucess, -1 for failure)...
const
MaxStrLength = 1024;
var
subStrStart: Integer;
idx: Integer;
lenStr: Integer;
tempString: String;
begin
Result := 1;
lenStr := Length(longString);
if (lenStr >= MaxStrLength) then
begin
Result := -1;
exit;
end;
//handle the possibility of
//there being a delimiter at
//the start of the string...
if (longString[1] = delimiter) then
begin
tempString := Copy(longString, 2, lenStr);
longString := tempString;
end;
subStrStart := 1;
for idx := 1 to lenStr do
begin
if ((longString[idx] = delimiter) or (idx = lenStr)) then
begin
if (idx <> lenStr) then
tempString := Copy(longString, subStrStart, (idx - subStrStart))
else
tempString := Copy(longString, subStrStart, ((idx - subStrStart) + 1));
subStrings.Add(tempString);
subStrStart := (idx + 1);
end;
end;
end;