Search code examples
javaregexstringreplaceall

replace a char in String


Hi I'd like to replace a char in a String. My problem is that at first you don't know which char it is, so in some cases I get an error message when my char is for example '+'. I don't want my char being interpreted as regex, so what should I do?

May code should be something like this:

String test = "something";
char ca = input.chatAt(0);
input = input.replaceAll("" + ca, "");

I hope you can help me.


Solution

  • Just don't use regex then.

    input = input.replace(String.valueOf(ca), "");
    

    The replaceAll method of String takes the String representation of a regular expression as an argument.

    The replace method does not.

    See API.