I have text being passed to a function. That text can contain anything, from a single character all the way to a full book.
I need to extract the first like and use it as a "title" so I can name a file where I am saving that text as a backup. I am using the following:
function GetTitle(var Text:string):string;
var
title: string;
position: integer;
begin
title := '';
position := AnsiPos(#10, Text);
if position = 0 then
begin
position := AnsiPos('.', Text);
if (position = 0) then
title := Text
else
title := copy(Text, 1, position);
end
else
begin
title := copy(Text, 1, position);
end;
result := title;
end;
I'm checking #10 and not #13 because the text can be either passed from a Windows app or a Mac OS X app. In cases where there is no #10 I'm checking for the 1st . (dot) and if there is none then I am passing the whole thing as a title.
This approach is causing some problems with filenames containing #13 in the name or names being too long. I can add a check for > 256 on title but that's just another thing.
Anyway, is there any way to properly read the first line (including #13#10 or #10 or #13 or . or whatever)?
I know this is simple, but I can't seem to find a way to deal with this without having nested ifs...
code is appreciated. thank you