Java indexOf method for multiple matches in String

前端 未结 6 1430
走了就别回头了
走了就别回头了 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:23

    There is a second variant of the indexOf method, which takes a start-index as a parameter.

    i = str.indexOf('x');
    while(i >= 0) {
         System.out.println(i);
         i = str.indexOf('x', i+1);
    }
    

提交回复
热议问题