Java indexOf method for multiple matches in String

前端 未结 6 1427
走了就别回头了
走了就别回头了 2020-11-29 06:24

I had a question about the indexOf method. I want to find multiple cases of \"X\" in a string.

Suppose my string is \"x is x is x is x\", I want to find x in all of

6条回答
  •  醉梦人生
    2020-11-29 07:12

    There's a another version of indexOf method, taking fromIndex as parameter.
    So, you can call it in a loop, each time passing prevPosition + 1 as a second parameter.

    Documentation:
    http://download.oracle.com/javase/6/docs/api/java/lang/String.html#indexOf(int, int)

提交回复
热议问题