rickard67 / Emmet-Pascal

Emmet components for use in Delphi or Lazarus
Other
10 stars 11 forks source link

GetLineCount #14

Closed Alexey-T closed 11 months ago

Alexey-T commented 11 months ago
function TEmmet.GetLineCount(const s: string): Integer;
var
  n: Integer;
begin
  Result := 1;
  n := Pos(#10,s);
  while n > 0 do
  begin
    Inc(Result);
    n := PosEx(#10,s,n+1);
  end;
end;

Simpler code can be using of S.CountChar(#10) + 1.

https://www.freepascal.org/docs-html/rtl/sysutils/tstringhelper.countchar.html (fpc) / https://docwiki.embarcadero.com/Libraries/Sydney/en/System.SysUtils.TStringHelper.CountChar (delphi).

or using this simple func.

function CountOfChar(ch: Char; const S: string): Integer;
var
  i: Integer;
begin
  Result := 0;
  for i := 1 to Length(S) do
    if S[i] = ch then
      Inc(Result);
end; 
rickard67 commented 11 months ago

Done!