如何替换Java中第一次出现的字符串
我想替换以下字符串中第一个出现的字符串
String test = "see Comments, this is for some test, help us"
**如果测试包含以下输入,则不应替换
- 请参见注释(末尾有空格)
- 见评论
- 见评论**
我想得到如下输出:
Output: this is for some test, help us
提前感谢,
你可以在下面搜索框中键入要查询的问题!
我想替换以下字符串中第一个出现的字符串
String test = "see Comments, this is for some test, help us"
**如果测试包含以下输入,则不应替换
我想得到如下输出:
Output: this is for some test, help us
提前感谢,
# 1 楼答案
使用^{} :
输出:
# 2 楼答案
你也可以使用这种方法
# 3 楼答案
您可以使用以下语句将第一次出现的文字字符串替换为另一个文字字符串:
然而,这在后台做了很多工作,而替换文字字符串的专用函数不需要这些工作
# 4 楼答案
为了编写自己的代码,您应该使用已经测试过且有良好文档记录的库
Javadoc
甚至还有一个版本不区分大小写(这很好)
马文
学分
我的答案是:https://stackoverflow.com/a/10861856/714112
# 5 楼答案
您可以使用以下方法
下面的link提供了使用正则表达式和不使用正则表达式替换第一个出现的字符串的示例
# 6 楼答案
可以使用字符串的^{} 方法