본문 바로가기

Delphi/꿀팁

Ansi로 된 한글 안깨지게 자르기

function CutKorAnsiToString(AString: AnsiString; Index: Integer): string;
var
  I, nCnt: Integer;
begin
  if Length(AString) <= Index then
    Result := AString
  else begin
    nCnt := 0;
    
    for I := 1 to Index do begin
      if (Integer(AString[I]) and Integer($80)) = Integer($80) then
        Inc(nCnt);
    end;
    
    if (nCnt mod 2) = 0 then
      Result := Copy(AString, 1, Index)
    else
      Result := Copy(AString, 1, Index - 1);
  end;
end;
반응형