Advertise with us

How to remove special characters from an string?

Posted by  Ashish Pandey
Java 
 1579  View(s)
Ratings:
Rate this:

I want to remove special character like:

- + ^ . : ,

from a string using java.

  1. Re: How to remove special characters from an string?

    Try replaceAll() method of the String class.

    BTW here is the method, return type and parameters.

    public String replaceAll(String regex, String replacement)

    Example:

    String str = "Hello +-^ my + - friends ^ ^^-- ^^^ +!";
    str = str.replaceAll("[-+^]*", "");

    It should remove all the {'^', '+', '-'} chars that you wanted to remove!

      Modified On Mar-31-2018 04:33:48 AM

Answer