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
- See Comments, (with space at the end)
- See comments,
- See Comments**
I want to get the output as follows,
Output: this is for some test, help us
Thanks in advance,
You can use following method.
Following link provide examples for replacing first occurrence of string using with and without regular expressions.
Use
substring(int beginIndex)
:OUTPUT:
You should use already tested and well documented libraries in favor of writing your own code.
Javadoc
There's even a version that is case insensitive (which is good).
Maven
Credits
My answer is an augmentation of: https://stackoverflow.com/a/10861856/714112
You can use following statement to replace first occurrence of string.
This link has complete program including test cases.
You can use
replaceFirst(String regex, String replacement)
method of String.Use String replaceFirst to swap the first instance of the delimiter to something unique: