Verify Valid Caracters

I´m making a program to change all of the caracters and numbers from any text file. But I need to verificate if the caracter that it´s going to change is a valid caracter before doing the changing.

Example of Valid Caraters and Numbers:
Letters from A to Z
Numbers 1 to 10
Caraters ",.:~/[?{=-)(*&¨%$#@!'/
Also the letters that are valid in my country like: Ç É â ã ú, etc..

Example of Invalid Caracter:
œƒ‹ßŠÐ€Ï ŠØ¹üÿ and more.

Is there a way to verify ?
Thanx
Fredy992Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kretzschmarCommented:
make a set with valid- or invalid chars like

const
  invalidChars : Set of char = ['œ','ƒ','‹', ........];

now you can easily compare like

if char in invalidchars then
  ....

just from head

meikl ;-)
0
andrewjbCommented:
var x : char

if x in ['a'..'z' , 'A'..'Z' , '0' .. '9' , '!"£$%" ]
...
0
Pierre CorneliusCommented:
This should work for Shortstring and AnsiString types but not for WideString (UniCode);


procedure ChangeChars(var s: string; CharsValid: TValidChars);
  function ChangeChar(c: char): char;
  begin
    result:= #0;
    case c of
      'a'..'z': result:= Upcase(c); //example: change all a..z to A..Z
      'A'..'Z': result:= '5'; //example: change all uppercase chars to 5
    end;
    if result = #0 then raise exception.Create('Character change not supported.');
  end;
var
  i,len: Integer;
begin
  len:= Length(s);
  for i := 0 to len-1 do
    if (s[i+1] in CharsValid)
      then s[i+1] := ChangeChar(s[i+1]); //change valid characters
      //else leave invalid characters as they are
end;


USAGE:

procedure TForm1.Button1Click(Sender: TObject);
var s: string;
begin
  s:= 'abcdefghijklmpopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  ChangeChars(s, ['A'..'z']);
  ShowMessage(s);
end;



Regards
Pierre Cornelius
0
Pierre CorneliusCommented:


procedure ChangeFile(fIn, fOut: TFileName; CharsValid: TValidChars);
const BufSize = 8096; //read in 8 kb blocks
var InFile, OutFile: TFileStream;
    s: string;
    BytesRead: integer;
begin
  InFile := TFileStream.Create(fIn, fmOpenRead);
  OutFile:= TFileStream.Create(fOut, fmCreate);

  try
    SetLength(s, BufSize);
    repeat
      BytesRead := InFile.Read(s[1], BufSize);
      ChangeChars(s, CharsValid);
      OutFile.WriteBuffer(s[1], BytesRead);
    until  BytesRead < BufSize;
  finally
    InFile.Free;
    OutFile.Free;
  end;
end;

USAGE:
=====
procedure TForm1.Button2Click(Sender: TObject);
begin
  ChangeFile('SomeFile.txt','NewSomeFile.txt',['A'..'z']);
end;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.