Jagadeesh Jagadeesh - 2 months ago 14
Java Question

How to replace first occurrence of string in Java

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

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


**If test contains the input as follows it should not replace


  1. See Comments, (with space at the end)

  2. See comments,

  3. See Comments**



I want to get the output as follows,

Output: this is for some test, help us


Thanks in advance,

Answer

You can use replaceFirst(String regex, String replacement) method of String.