Substituir caracteres especiais em Strings Delphi

O que ando aprendendo nesta caminhada sobre a Terra

Substituir caracteres especiais em Strings Delphi

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
    begin
      pos := 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

  1. Marco says:

    Could put it right? and untranslated
    It's even better to understand, do que tentar decifrar o que é “;para eu”; e “;final”;

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.