Java - Write code to remove all special characters by replacing

Requirements

Write code to remove all special characters by replacing

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String orig = "book2s.com";
        System.out.println(removeSpecialCharacters(orig));
    }//from   w w  w. j  ava 2s  .co  m

    /**
     * this method will remove all special characters
     * @param orig
     * @return
     */
    public static String removeSpecialCharacters(String orig) {
        if (orig == null)
            return "";
        else {
            // replacing with space allows the camelcase to work a little better in most cases.
            orig.replace('\\', ' ');
            orig.replace('(', ' ');
            orig.replace(')', ' ');
            orig.replace('/', ' ');
            orig.replace('-', ' ');
            orig.replace(',', ' ');
            orig.replace('>', ' ');
            orig.replace('<', ' ');
            orig.replace('-', ' ');
            orig.replace('&', ' ');

            // single quotes shouldn't result in CamelCase variables like Patient's -> PatientS
            // "smart" forward quote
            orig.replace("'", "");

            orig.replace("\u2019", ""); // smart forward (possessive) quote.

            // make sure to get rid of double spaces.
            orig.replace("   ", " ");
            orig.replace("  ", " ");

            orig.trim(); // Remove leading and trailing spaces.

            return (orig);
        }
    }
}

Related Exercise