【问题标题】:removal of double quotes in a sentence去除句子中的双引号
【发布时间】:2014-08-27 03:31:46
【问题描述】:

我有这样的文字:

“@joemcelderry91: I've woken up with the flu I think! :( gunna try and
run it off haha!! X” get well soon joey :)

我想去掉句子中的双引号。删除后应该是这样的

@joemcelderry91: I've woken up with the flu I think! :( gunna try and
run it off haha!! X get well soon joey :)

到目前为止,有没有人建议删除这个我已经尝试过这个仍然没有运气:

.replaceAll("\"", "");

【问题讨论】:

标签: java regex text


【解决方案1】:

您似乎包含了不同的引号。所以你的正则表达式是,

"[“”\"]"

用空字符串替换它。

DEMO

如果是特殊的双引号,则使用以下代码将其从字符串中删除,

String str = "“@joemcelderry91: I've woken up with the flu I think! :( gunna try and run it off haha!! X” get well soon joey :)";
String s = str.replaceAll("[“”]", "");
System.out.println(s);

输出:

@joemcelderry91: I've woken up with the flu I think! :( gunna try and run it off haha!! X get well soon joey :)

IDEONE

【讨论】:

  • 感谢 Avinash 发现我的问题,这是一个特殊的双引号,我无法摆脱它。
【解决方案2】:

调用replaceAll() 对字符串没有任何作用,因为字符串是不可变的。相反,replaceAll()返回修改后的字符串,所以赋值返回值:

str = str.replaceAll("\"", "");

另外...不要使用正则表达式!!!您的搜索词和替换词都不需要正则表达式,因此请使用非正则表达式版本的替换:

str = str.replace("\"", "");

请注意,尽管其名称中没有“all”,replace() 仍会替换所有出现的位置。

【讨论】:

    【解决方案3】:

    您遇到的错误是什么?这对我有用:

    String str = "@joemcelderry91: I've woken up with the flu I think! :( gunna try and run it off haha!! X\" get well soon joey :)";
    System.out.println(str.replaceAll("\"", ""));

    给我输出:

    @joemcelderry91:我觉得我是感冒了! :( gunna try and run it off haha​​!! X 快点好起来吧,乔伊 :)

    【讨论】:

      猜你喜欢
      • 2014-04-07
      • 2010-11-13
      • 2023-04-03
      • 2011-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-06
      • 1970-01-01
      相关资源
      最近更新 更多