Solved

Given a string, I want to delete all the "weird" characters and return a "cleaned" string

Posted on 2011-03-08
6
386 Views
Last Modified: 2012-05-11
['0'..'9','A'..'Z','a'..'z','?','.','>','<','+','-','~','!','@','#','$','%','&','*','(',')','_','=','{','}','[',']','|','\','/',':'­,';',' ']

This seems to be a common problem and I can't seem to find the right solution.

I would like to have ONLY the above characters permissible in a string.

So I guess I just need a function that will ensure that happens...?

I'm using Delphi 2006

Also, I always face this "weird character" problem.

Does anyone know of a text editor (like notepad) into which I can paste text and then actually SEE (somehow...) what the code/number of the character that is being represented.

0
Comment
Question by:bobbysdog
  • 3
  • 2
6 Comments
 
LVL 32

Expert Comment

by:ewangoya
ID: 35078838
use this



//test
mystring := CleanString(myString)

function CleanString(const AValue: string): string;
var
  I: Integer;
begin
  Result := '';
  for I := 1 to Length(AValue) do
    if AValue[I] in ['0'..'9','A'..'Z','a'..'z','?','.','>','<','+','-','~','!','@','#','$','%','&','*','(',')','_','=','{','}','[',']','|','\','/',':'­,';',' '] then
      Result := Result + AValue[I];
end;

Open in new window

0
 
LVL 32

Accepted Solution

by:
ewangoya earned 500 total points
ID: 35078845
You missed a comma in your list
function CleanString(const AValue: string): string;
var
  I: Integer;
begin
  Result := '';
  for I := 1 to Length(AValue) do
    if AValue[I] in ['0'..'9','A'..'Z','a'..'z','?','.','>','<','+','-','~','!','@','#','$','%','&','*','(',')','_','=','{','}','[',']','|','\','/',':', '­',';',' '] then
      Result := Result + AValue[I];
end;

Open in new window

0
 
LVL 32

Expert Comment

by:ewangoya
ID: 35078942

In fact a better way to do it and support unicode as well is to define a character set first. Don't use the one in Delphi
//Test

procedure TForm3.Button1Click(Sender: TObject);
var
  goodstring: string;
begin
  goodstring := CleanString('Çy,129AXcfv45ü130é131â132ä');
  ShowMessage(goodstring);
end;

Open in new window

type
  TAllowedChars = set of Char;

const
  CAllowedChars: TAllowedChars =
    ['0'..'9','A'..'Z','a'..'z','?','.','>','<','+','-','~','!','@','#','$','%','&','*','(',')','_','=','{','}','[',']','|','\','/',':', '­',';',' '];

function CleanString(const AValue: string): string;
var
  I: Integer;
begin
  Result := '';
  for I := 1 to Length(AValue) do
    if AValue[I] in CAllowedChars then
      Result := Result + AValue[I];
end;

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Closing Comment

by:bobbysdog
ID: 35079108
Wooo Hoo SAVIOR of MANKIND!!!!!

Thanks!!

You're a genius!

For people who come to this thread in the future, my original character set left out a few important ones like commas and quotation marks and stuff.

So use the below code to include those:

[code]
function CleanString(const AValue: string): string;
var
  I: Integer;
begin
  Result := '';
  for I := 1 to Length(AValue) do
    if AValue[I] in ['0'..'9','A'..'Z','a'..'z','"','?','.',',','>','<','+','-','~','!','@','#','$','%','&','*','(',')','_','=','{','}','[',']','|','\','/',':', '­',';','''', ' '] then
      Result := Result + AValue[I];
end;
[/code]
0
 

Author Comment

by:bobbysdog
ID: 35079127
Oops, the CODE function didn't work up there.

here ya go...
function CleanString(const AValue: string): string;
var
  I: Integer;
begin
  Result := '';
  for I := 1 to Length(AValue) do
    if AValue[I] in ['0'..'9','A'..'Z','a'..'z','"','?','.',',','>','<','+','-','~','!','@','#','$','%','&','*','(',')','_','=','{','}','[',']','|','\','/',':', '­',';','''', ' '] then
      Result := Result + AValue[I];
end;

Open in new window

0
 
LVL 45

Expert Comment

by:aikimark
ID: 35082450
@bobbysdog

Is there any character in the 32..126 range that isn't acceptable?

How long are these strings that you need to 'clean'?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now