[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-03-08
6
Medium Priority
?
394 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: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 2000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 46

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

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!

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
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

831 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