2014-09-11 89 views
-3

如何替换下面的字符串。如何从字符串中删除单引号,使用替换

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); 
+1

你的问题标题违背你的榜样。 – raina77ow 2014-09-11 06:37:16

+0

您不必在字符串文字中跳过撇号。一个(单个)空间不是没有字符的空字符串(又名“空字符串”)。 – laune 2014-09-11 06:40:32

+1

你的问题说“空格”并不意味着“”。 – 2014-09-11 06:42:11

回答

2

当你要替换'与空间,你为什么不这样做呢?

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

也试过这个但没用。 :( – user2432358 2014-09-11 06:35:03

+1

这不是真的,只是运行它,并获得'new text :: First American s style'作为结果。 – Smutje 2014-09-11 06:35:47

+0

您认为用'Text s'代替'Text's'有什么意义吗? – raina77ow 2014-09-11 06:38:06

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

test = test.replace("\'", " "); 
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 
+0

是的,它的工作很好,谢谢你:) – user2432358 2014-09-11 06:37:39

+0

我真的觉得这个问题已经被破坏,因为OP提到他确实尝试过'replace.All(“'”,“”)''。不过,你是唯一一个试图理解OP真正需要的东西(*而不是他应得的*),因此我是+1。 ) – raina77ow 2014-09-11 06:40:12

+0

@ user2432358如果有效,请接受此答案。 – Jens 2014-09-11 07:21:24

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("'", "")); 
相关问题