Stef Merlijn
asked on
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
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
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;
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;
ASKER
Hi Emil, Thanks.
Do you also know how to do that with a text in a memo?
Do you also know how to do that with a text in a memo?
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;
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;
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.Te xt) );
end;
uses StrUtils;
procedure TForm1.Button1Click(Sender
function MaxLongLine(const TheString:string):integer;
var
iPos : integer;
Offset : integer;
begin
Result:=0;
Offset:=0;
repeat
iPos:=PosEx(#13,TheString,
if (iPos-Offset)>Result then Result:=iPos-Offset;
OffSet:=Succ(iPos);
until (iPos=0);
Result:=Pred(Result);
end;
begin
Caption:=IntToStr ( MaxLongLine(Memo1.Lines.Te
end;
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;
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;
Excuse me, it should be:
P[0] := 0;
P[1] := 0;
P[0] := 0;
P[1] := 0;
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;
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;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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;