Solved

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

Posted on 2011-03-08
6
390 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 32

Expert Comment

by:Ephraim Wangoya
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:
Ephraim Wangoya 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:Ephraim Wangoya
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

695 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