# URGENT: Determine the length of a string based on the longest line.

Hi,

I want to determine the longest length of a string based on the longest line within the string.
Suppose I have following string:

'This is a very long line that hasn't got a CR in it. Therefor the length will be very long.';
In this case the length will be f.e. 300

'This is a very long ' + chr(13)
+ 'line that has got a CR in it.' + chr(13)        << This is the longest line within the string.
+ 'Therefor the length ' + chr(13)
+ 'will be less long.';
In this case the length will be f.e. 70

Is there a way to calculate this? Just like the messagebox and messagedlg probably do?
Please supply me with some code.

Regards,
Stef
DeveloperAsked:
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
function Longest_Line(S: string): Integer;
var
I:     Integer;
SL:    TStringList;
begin
Result := 0;
SL := TStringList.Create;
try
SL.Text := S;
for I := 0 to SL.Count-1 do
if (Length(SL[I])>Result) then
Result := Length(SL[I]);
finally
SL.Free;
end;
end;
Commented:
or faster:

function Longest_Line(S: string): Integer;
var
I:     Integer;
L:     Integer;
SL:    TStringList;
begin
Result := 0;
SL := TStringList.Create;
try
SL.Text := S;
for I := 0 to SL.Count-1 do
begin
L := Length(SL[I]);
if (L>Result) then
Result := L;
end;
finally
SL.Free;
end;
end;
DeveloperAuthor Commented:
Hi Emil, Thanks.

Do you also know how to do that with a text in a memo?
Commented:
function Longest_Memo_Line(Memo: TMemo): Integer;
var
I:     Integer;
L:     Integer;
begin
Result := 0;
for I := 0 to Memo.Lines.Count-1 do
begin
L := Length(Memo.Lines[I]);
if (L>Result) then
Result := L;
end;
end;
Commented:
faster:

uses StrUtils;

procedure TForm1.Button1Click(Sender: TObject);

function MaxLongLine(const TheString:string):integer;
var
iPos    : integer;
Offset  : integer;
begin
Result:=0;
Offset:=0;
repeat
iPos:=PosEx(#13,TheString,Offset);
if (iPos-Offset)>Result then Result:=iPos-Offset;
OffSet:=Succ(iPos);
until (iPos=0);
Result:=Pred(Result);
end;

begin
Caption:=IntToStr ( MaxLongLine(Memo1.Lines.Text) );
end;
Commented:
Hi Stef, this is a fast way:

function  Fast_Longest_Memo_Line(S: string): Integer;
var
I:      Integer;
L:      Integer;
P:      array[0..1] of Integer;
begin
P := 1;
P := 1;
Result := 0;
for I := 1 to Length(S) do
begin
if (S[I]=#13) then
begin
P := P;
P := I;
L := P-P-2;
if (L>Result) then
Result := L;
end;
end;
end;
Commented:
Excuse me, it should be:
P := 0;
P := 0;
Commented:
function  Fast_Longest_Memo_Line(S: string): Integer;
var
I:      Integer;
L:      Integer;
P:      array[0..1] of Integer;
begin
P := 0;
P := 0;
Result := 0;
for I := 1 to Length(S) do
begin
if (S[I]=#13) then
begin
P := P;
P := I;
L := P-P-2;
if (L>Result) then
Result := L;
end;
end;
end;
Commented:
This is a correct and a fast solution (tested - it works fine when the loggest line is the first, middle one or the last one):

function  Fast_Longest_Memo_Line(S: string): Integer;
var
I:      Integer;
J:      Integer;
K:      Integer;
L:      Integer;
M:      Integer;
begin
L := Length(S);
M := (Pos(#13, S));
if (M=0) then
Result := L
else
begin
Result := M - 1;
if (M<L) then
begin
J := M + 1;
for I := J to L do
begin
if (S[I]=#13) then
begin
K := I - M - 2;
M := I;
if (K>Result) then
Result := K;
end;
end;
if (M<L) then
begin
K := L - M - 1;
if (K>Result) then
Result := K;
end;
end;
end;
end;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.