假设我们有这样的东西:
&firstString=someText&endString=OtherText
我想用其他东西替换“someText”。考虑到我不知道 someText 可能是什么(任何字符串)并且我所知道的是它将被 &firstString= 和 &endString=
包围,最好的方法是什么编辑:抱歉,这看起来不够清楚。我不知道“someText”可能是什么,我唯一的信息是它将在 &firstString= 和 &endString= 之间
我曾多次考虑使用 split,但听起来很难看..
最佳答案
您可以使用 String#replaceAll支持这样的正则表达式:
String newstr = str.replaceAll("(&firstString=)[^&]*(&endString=)", "$1foo$2");
关于java - 替换两个字符串之间的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10403639/