How to replace ' with empty string in Java

﹥>﹥吖頭↗ 提交于 2019-12-02 04:19:43

Strings are immutable so you'll receive a new instance. Try

 data = data.replace("'", "");

Your Edit

Check the return values of getString() - you could get your NPE because your database table contains null values in one of the columns spl_char or replace_with.

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("'", "");

I can see your problem, If you need to replace it you need to replace it and also need to assign it back to the variable. Solution should be,

        String data="Sid's den";

        data = data.replaceAll("'", "");

        System.out.println(data);

Because String is immutable in java. But StringBuffer and StringBuilder is mutable in java.

String

StringBuffer

StringBuilder

Just try with:

"foo'bar'buz".replace("'", "")

Output:

"foobarbuz"

In your case:

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

Try:

data = data.replace ("'", "") ;

OR

data = data.replaceAll("'", "") ;

You would need to assign the replaced string to a variable.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!