如何替换下面的字符串。
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);
答案 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("\'", " ");