Today I had to modify an ancient system done in Delphi to generate files containing some information. Nestas informações há elementos que se utilizam de caracteres não Alpha-numéricos como “;:; e “;/; and behold, to my surprise I am obligated to remove them.

Procurei por uma função que substituísse caracteres como “;Ç”; para “;C”; and only found removal functions.

Then I created my that is just below.

{
  Replaces special characters for ASCII equivalents
}
Function ReplaceNonAscii(const s: String) : String;
var i, POS: Integer;
const undesiredchars : String = '/ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÜÚÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùüúþÿ';
const replaces : String = '  AAAAAAACEEEEIIIIDNOOOOOxOUUUbBaaaaaaaceeeeiiiionooooo ouuuby';
Begin SetLength(Result, Length(s));
  for i := 1 to Length(s) Do start then := ord(s[i]);
      if (s[i] in [#32, #48..#57, #65..#90, #97..#122]) then Result[i] := s[i]
      else begin pos := AnsiPos(s[i], undesiredchars);
          Result[i] := replaces[POS + 1];
        end;
    end;

end;

2 responses to “Substituir caracteres especiais em Strings Delphi

  1. Marco Avatar
    Marco

    Poderia colocar de forma certa? e não traduzido
    Fica até melhor para entender, do que tentar decifrar o que é “para eu” e “final”

    1. Marcos Regis Avatar
      Marcos Regis

      Não entendi o questionamento.

Leave a Reply

Your email address will not be published. Required fields are marked *