Length of a UTF8String in Delphi

Viewed 361

how do i get the number of visible charcters in a UTF8String regardless of how many bytes it takes to hold the string?

1 Answers

If you under Windows, try CharNextW:

uses
  Winapi.Windows,
  System.SysUtils;

function GetCharsCount(const AStr: UTF8String): Integer;
var
  P: PWideChar;
begin
  Result := 0;
  P := PWideChar(UnicodeString(AStr));
  while P <> '' do begin
    Inc(Result);
    P := CharNextW(P);
  end;
end;
Related