Better way to remove special characters from a string

Posted on 2005-05-06
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:
            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
    LVL 6

    Accepted Solution

    you can use System.Text.RegularExpressions.Regex.Replace(input, pattern, replacement) function which returns the updated string.
    LVL 6

    Expert Comment

    by:Neeraj Soni
    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.
    LVL 1

    Author Comment

    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_] ??

    LVL 3

    Expert Comment

    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

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now