Better way to remove special characters from a string

Posted on 2005-05-06
Medium Priority
Last Modified: 2011-08-18
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.



Question by:kellycoinguy
  • 2

Accepted Solution

Neeraj Soni earned 500 total points
ID: 13950494
you can use System.Text.RegularExpressions.Regex.Replace(input, pattern, replacement) function which returns the updated string.

Expert Comment

by:Neeraj Soni
ID: 13950502
for example,
System.Text.RegularExpressions.Regex.Replace("0we-are-making-it-happen1","[-0-9]"," ");


we are making it happen

All hyphans and numbers are replaced with space.

Author Comment

ID: 14023058
Yes, I'm well aware of the Regex.Replace... so what is the regular expression for all non A-Z a-z _ chars? ^[A-Za-z_] ??


Expert Comment

ID: 22067718
It is actually [^a-zA-Z_]
I tried it and this works - the caret ^ needs to be inside the bracket for this to work.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month16 days, 21 hours left to enroll

862 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