How to replace first occurrence of string in Java

后端 未结 6 1042
悲&欢浪女
悲&欢浪女 2020-11-29 07:20

I want to replace first occurrence of String in the following.

  String test = \"see Comments, this is for some test, help us\"

**If test c

6条回答
  •  一个人的身影
    2020-11-29 07:41

    You can use following statement to replace first occurrence of literal string with another literal string:

    String result = input.replaceFirst(Pattern.quote(search), Matcher.quoteReplacement(replace));
    

    However, this does a lot of work in the background which would not be needed with a dedicated function for replacing literal strings.

提交回复
热议问题