如何使用replaceAll删除部分文本

Bur*_*ras 1 java replaceall

有一个String text System.out.println(text);看起来像这样

So the traveller sat down by the side of that old man,
face to face with the serene sunset; 
and all his friends came softly back and stood around him. 
Run Code Online (Sandbox Code Playgroud)

另一个 String subText

System.out.println(subText); 只是上述字符串的一部分,看起来像这样

So the traveller sat down by the side of that old man,
face to face with the serene sunset;
Run Code Online (Sandbox Code Playgroud)

我需要摆脱这subText部分,text = text.replaceAll(subtext, "");但这对文本没有任何作用?

Jon*_*oni 6

在这种特殊情况下它没关系,但你真的应该使用replace而不是replaceAll:

text = text.replace(subtext, "");
Run Code Online (Sandbox Code Playgroud)

replaceAll方法使用正则表达式,有些字符具有特殊含义.

在这种特殊情况下,你不会看到任何差异,因为必须有一些微妙的不同 subtext,所以它无法找到text.也许有额外的空格或换行符的编码方式不同.很难看出白色空间的差异看着产生的输出System.out.println.