Solved

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

Posted on 2011-03-08
6
385 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
Comment Utility
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
Comment Utility
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
Comment Utility

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Closing Comment

by:bobbysdog
Comment Utility
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
Comment Utility
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
Comment Utility
@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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 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

12 Experts available now in Live!

Get 1:1 Help Now