Advertisement

05.06.2005 at 03:05PM PDT, ID: 21415748
[x]
Attachment Details

Better way to remove special characters from a string

Asked by kellycoinguy in C# Programming Language

Tags: special, remove, characters, string, from

This is my lame excuse for a routine to remove special characters from a string (I just want the spaces preserved so I can then turn it into a CamelCase variable name... is there a better way?

      static public string removeSpecialCharacters(string orig)
      {
            string rv;

            // replacing with space allows the camelcase to work a little better in most cases.
            rv = orig.Replace("\\"," ");
            rv = rv.Replace("("," ");
            rv = rv.Replace(")"," ");
            rv = rv.Replace("/"," ");
            rv = rv.Replace("-"," ");
            rv = rv.Replace(","," ");
            rv = rv.Replace(">"," ");
            rv = rv.Replace("<"," ");
            rv = rv.Replace("-"," ");
            rv = rv.Replace("&"," ");

            // single quotes shouldn't result in CamelCase variables like Patient's -> PatientS
            // "smart" forward quote
            rv = rv.Replace("'","");
            
            // if you have to find any more weird unicode chars, look here:
            // http://seth.positivism.org/man.cgi/7/groff_char
            rv = rv.Replace("\u2019",""); // smart forward (possessive) quote.

            // make sure to get rid of double spaces.
            rv = rv.Replace("   "," ");
            rv = rv.Replace("  "," ");

            rv = rv.Trim(' '); // Remove leading and trailing spaces.

            return(rv);
      }

Thanks!

-KellyStart Free Trial
[+][-]05.07.2005 at 05:42AM PDT, ID: 13950494

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: C# Programming Language
Tags: special, remove, characters, string, from
Sign Up Now!
Solution Provided By: neerajsoni
Participating Experts: 2
Solution Grade: C
 
 
[+][-]05.07.2005 at 05:46AM PDT, ID: 13950502

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.17.2005 at 03:37PM PDT, ID: 14023058

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.13.2005 at 05:41PM PDT, ID: 14207703

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32