[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register 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
?
393 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 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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 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 course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

649 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