Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Verify Valid Caracters

Posted on 2004-11-11
4
Medium Priority
?
285 Views
Last Modified: 2010-04-05
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
0
Comment
Question by:Fredy992
  • 2
4 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 12553950
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
 
LVL 12

Expert Comment

by:andrewjb
ID: 12554035
var x : char

if x in ['a'..'z' , 'A'..'Z' , '0' .. '9' , '!"£$%" ]
...
0
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 12554462
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
 
LVL 14

Accepted Solution

by:
Pierre Cornelius earned 750 total points
ID: 12554930


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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question