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
Stef MerlijnDeveloperAsked:
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.

esoftbgCommented:
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;
esoftbgCommented:
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;
Stef MerlijnDeveloperAuthor Commented:
Hi Emil, Thanks.

Do you also know how to do that with a text in a memo?
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

esoftbgCommented:
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;
RadikalQ3Commented:
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;
esoftbgCommented:
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[0] := 1;
  P[1] := 1;
  Result := 0;
  for I := 1 to Length(S) do
  begin
    if (S[I]=#13) then
    begin
      P[0] := P[1];
      P[1] := I;
      L := P[1]-P[0]-2;
      if (L>Result) then
        Result := L;
    end;
  end;
end;
esoftbgCommented:
Excuse me, it should be:
  P[0] := 0;
  P[1] := 0;
esoftbgCommented:
function  Fast_Longest_Memo_Line(S: string): Integer;
var
  I:      Integer;
  L:      Integer;
  P:      array[0..1] of Integer;
begin
  P[0] := 0;
  P[1] := 0;
  Result := 0;
  for I := 1 to Length(S) do
  begin
    if (S[I]=#13) then
    begin
      P[0] := P[1];
      P[1] := I;
      L := P[1]-P[0]-2;
      if (L>Result) then
        Result := L;
    end;
  end;
end;
esoftbgCommented:
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.

Start your 7-day free trial
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.