Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace ' with empty string in Java

How can I replace single quote ' with empty string in Java. I tried following but doesn't seem to be working.

String data="Sid's den";
data.replace("'", "");
data.replaceAll("'", "");

Thanks in advance. Any help is much appreciated.(Output should be: Sids den)

Thanks guys for your responses. I guess I should have been more clear about my question. Basically I am getting special characters from the table and with what value we have to replace that also from the same table. Here is snippet of the code:

query = "select spl_char, replace_with from entcon_splchars";
ptsmt = DBConnector.sqlConnection.prepareStatement(query);
rs = ptsmt.executeQuery();

while (rs.next()) {
        if(data.contains(rs.getString("spl_char"))){
          data = data.replace(rs.getString("spl_char"),rs.getString("replace_with"));
            }
         }

so whenevr in the data we have special character ' then I am facing nullpointer exception. Please suggest how to go ahead with this?

like image 726
Sid Avatar asked Dec 02 '22 19:12

Sid


1 Answers

Use replace, no need for regex.

Remember that Strings are immutable, so you need to assign data.replace("'", ""); to a variable.

For instance: data = data.replace("'", "");

like image 175
Mena Avatar answered Dec 12 '22 11:12

Mena