



Quem trabalha com arquivos texto, muitas vezes se depara com a
necessidade de remover acentos das palavras.
A boa notícia é que existe uma função muito prática
para resolver este problema.
É necessário estar declarado SysUtils na seção uses,
em versões unicode declare System.SysUtils;
Abaixo segue o código fonte da função:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | function RemoveAcento(aText : string) : string; const ComAcento = 'àâêôûãõáéíóúçüñýÀÂÊÔÛÃÕÁÉÍÓÚÇÜÑÝ'; SemAcento = 'aaeouaoaeioucunyAAEOUAOAEIOUCUNY'; var x: Cardinal; begin; for x := 1 to Length(aText) do try if (Pos(aText[x], ComAcento) <> 0) then aText[x] := SemAcento[ Pos(aText[x], ComAcento) ]; except on E: Exception do raise Exception.Create('Erro no processo.'); end; Result := aText; end; |
Outra sugestão, mais recomendável, enviada por Ivan Cesar
1 2 3 4 5 6 | function RemoveAcento(const pText: string): string; type USAscii20127 = type AnsiString(20127); begin Result := string(USAscii20127(pText)); end; |
Exemplos de uso
1 2 3 4 5 6 7 8 9 10 11 12 | procedure TForm1.Button1Click(Sender: TObject); var vTexto : String; begin { Exemplo 1 - Com variáveis } vTexto := 'João é um aluno de Delphi.'; vTexto := RemoveAcento(vTexto); ShowMessage(vTexto); { Exemplo 2 - String Estática. } ShowMessage(RemoveAcento('A ÁRVORE É DE MAÇA')); end; |
Você precisa fazer o login para publicar um comentário.
function StripAccents(const AInputString : String): String;
const
_US_ASCII_CODEPAGE = 20127 ; // us ascii
{$IFDEF UNICODE}
type
USASCIIString = type AnsiString(_US_ASCII_CODEPAGE) ;
{$ELSE}
var
LWideString : WideString;
{$ENDIF}
begin
{$IFDEF UNICODE}
Result := String(USASCIIString(aStr));
{$ELSE}
LWideString := WideString(AInputString);
SetLength(Result, WideCharToMultiByte(_US_ASCII_CODEPAGE, 0, PWideChar(LWideString),
Length(LWideString), nil, 0, nil, nil));
WideCharToMultiByte(_US_ASCII_CODEPAGE, 0, PWideChar(LWideString), Length(LWideString),
PChar(Result), Length(Result), nil, nil);
{$ENDIF}
end;
Testei as duas funções e ambas são excelentes!
muito obrigado