Java replace only first occurrence of a substring in a string

后端 未结 3 654
南笙
南笙 2020-12-11 20:23

This is somehow a duplicate of this problem Ruby - replace the first occurrence of a substring with another string just in java.

Problem is:

I have a string:

3条回答
  •  粉色の甜心
    2020-12-11 21:13

    Try the replaceFirst method. It uses a regular expression, but the literal sequence "ha" still works.

    string.replaceFirst("ha", "gurp");
    

提交回复
热议问题