如何使用replace从字符串中删除单引号

时间:2014-09-11 06:32:27

标签: java android replace

如何替换下面的字符串。

String test =  "This is my Test's cases";

现在我正在取代"'"空间就像这样"这是我的测试案例" 我试过了:

  1) replace("'","")
  2) replaceAll("'","")
  3) replace("\'","")

...但我没有得到任何必要的结果。

测试代码:

String test = "The First American's style";
System.out.println("old text::"+test);      
test = test.replaceAll("'","\\'");
System.out.println("new text::"+test);

5 个答案:

答案 0 :(得分:6)

这很好用:

    String test = "The First American's style";
    System.out.println("old text::"+test);      
    test = test.replaceAll("'","");
    System.out.println("new text::"+test);

输出:

old text::The First American's style
new text::The First Americans style

答案 1 :(得分:2)

如果您想用空格替换',为什么不这样做呢?

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

答案 2 :(得分:0)

StringBuilder sb = new StringBuilder("Hey replace string's name.");
System.out.println("Old String::"+sb.toString());      
System.out.println("New String::"+sb.toString().replaceAll("'", ""));

答案 3 :(得分:0)

使用ASCII,unicode值替换单引号..

    test = test.replaceAll((char)145,"");
otherwise  
     test = test.replaceAll(\u0091,"");

了解更多信息http://www.fileformat.info/info/unicode/char/0091/index.htm

完成了享受......

答案 4 :(得分:-1)

test = test.replace("\\'", " ");

test = test.replace("\'", " ");