java 替换java中的字符序列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10369837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:44:15  来源:igfitidea点击:

Replace sequence of characters in java

java

提问by user1154644

I am parsing a poorly structured rss feed, and some of the data that is being returned has <p>atin it. How can I replace all instance of <p>atwith an empty space, using java?

我正在解析一个结构不佳的 rss 提要,并且其中包含一些返回的数据<p>at。如何<p>at使用java用空白替换所有实例?

I'm familiar with the .replacemethod for the String class, but I'm not sure how the regex expression would look. I tried inputString.replace("<p>at", "")but that didn't work.

我熟悉.replaceString 类的方法,但我不确定正则表达式的外观。我试过了,inputString.replace("<p>at", "")但这没有用。

回答by óscar López

Try this:

试试这个:

inputString = inputString.replace("<p>at", "");

Be aware that the replace()method does notmodify the Stringin-place (as is the case with allmethods in the Stringclass, because it's immutable), instead it returns a new Stringwith the modifications - and you need to save the returned string somewhere.

请注意,replace()方法并没有改变String就地(这是与本案所有的方法String类,因为它是不可变的),相反,它返回一个新的String与改进-你需要保存返回的字符串的地方。

Also, the above version of replace()doesn't receive a regular expression as an argument, just the string to be replaced and its replacement.

此外,上面的版本replace()不接收正则表达式作为参数,只接收要替换的字符串及其替换。

回答by Subhrajyoti Majumder

inputString.replace("<p>at", "") // this will replace all match's with second parameter charsequence
inputString.replaceAll("<p>at", "") //  Replaces each substring of this string that matches the given regular expression with the given replacement.

you can use anyone.

你可以使用任何人。

String newInputString = inputString.replaceAll("<p>at", "");

thanks

谢谢