Solved

Error in Synax how can I only allow normal chars within a string?

Posted on 2011-03-02
10
154 Views
Last Modified: 2012-05-11
the system only allows normal txt though doesn't allow /?\* etc, if the string has stuff how can Ionly allow A-Z 0-9?? and it would delete the bad chars while leaving the good ones still in the string.
0
Comment
Question by:eNarc
  • 2
  • 2
  • 2
  • +2
10 Comments
 
LVL 8

Expert Comment

by:BdLm
ID: 35016199

you could check the ascii value and reject values out of range  ...
0
 
LVL 19

Accepted Solution

by:
Thommy earned 168 total points
ID: 35016213
Use this function to strip non alphanumeric chars from a string...
function DeleteNonAlphaNumChars(const TheString: string):string;
var
  SrcP, DestP: PChar; 
begin
  SrcP := PChar(TheString);
  SetLength(Result, Length(TheString));
  DestP := PChar(Result);
  while SrcP <> #0 do begin
    if SrcP[0] in ['a'..'z', 'A'..'Z', '0'..'9'] then begin
      DestP[0] := SrcP[0];
      Inc(DestP);
    end;
    Inc(SrcP);
  end; 
  SetLength(Result, DestP - PChar(Result)); 
end;

Open in new window

0
 
LVL 8

Expert Comment

by:BdLm
ID: 35016221
http://en.wikipedia.org/wiki/ASCII   as a reference ascii table

'A' ..... 'X'   -> ascii rannge 65 ....90    ord('A')  gives you the ascii value 65
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 24

Assisted Solution

by:jimyX
jimyX earned 166 total points
ID: 35016276
You can check if a string is in the valid set of chars:
Function DelSpecialChar(temp:String):String;
var
  i : integer;
begin
  result:='';
  for i:= 1 to length(temp) do
    begin
      if temp[i] in ['A'..'Z','a'..'z','0'..'9'] then
        result:= result + temp[i]
    end;
end;

Open in new window

   
0
 
LVL 19

Expert Comment

by:Thommy
ID: 35016345
For performance reasons (highest speed) if have used PCHAR() in function DeleteNonAlphaNumChars...
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35016357
So you can use it this way, let's say your string Str is including the special characters:

Str := 'e/?Na\*rc';
Str := DelSpecialChar(Str);
Showmessage(Str);

Open in new window

0
 
LVL 32

Expert Comment

by:ewangoya
ID: 35019351
try this code
function IsValidChar(const S: char): Boolean ;
begin
  Result := S in ['A'..'Z', 'a'..'z'];
end;

function NormaLizeStr(const AStr: string): string;
var
  I: Integer;
begin
  Result := AStr;
  for I := Length(Result) downto 1 do
    if not IsValidChar(Result[I]) then
      Delete(Result, I, 1);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(NormaLizeStr('*(kiuer)lp/\'));
end;

Open in new window

0
 
LVL 32

Assisted Solution

by:ewangoya
ewangoya earned 166 total points
ID: 35173840

This is even faster using CharInSet
function IsValidChar(const S: char): Boolean ;
begin
  Result := CharInSet(S, ['A'..'Z', 'a'..'z', '0'..'9']);
end;

function NormaLizeStr(const AStr: string): string;
var
  I: Integer;
begin
  Result := AStr;
  for I := Length(Result) downto 1 do
    if not IsValidChar(Result[I]) then
      Delete(Result, I, 1);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(NormaLizeStr('*(kiuer)lp/\'));
end;

Open in new window

0
 
LVL 100

Expert Comment

by:mlmcc
ID: 35399280
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

839 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