查找字符串中多个字符出现的位置

时间:2021-11-17 19:16:51

If I have a string line = "XYZTGEXGXRX", line.indexOf("X"); returns the index of the first "X" that is contained in that string.

如果我有一个字符串行=“XYZTGEXGXRX”,则line.indexOf(“X”);返回该字符串中包含的第一个“X”的索引。

What I want to know, is what what would allow me to get the second "X", or any occurrence of "X" after that?

我想知道的是,是什么让我得到第二个“X”,或之后出现的“X”?

1 个解决方案

#1


Answer can be found here: Java indexOf method for multiple matches in String

答案可以在这里找到:Java indexOf用于String中多个匹配的方法

There is a second parameter for indexOf which sets a start parameter. This code example prints all indices of x

indexOf有第二个参数,用于设置起始参数。此代码示例打印x的所有索引

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

#1


Answer can be found here: Java indexOf method for multiple matches in String

答案可以在这里找到:Java indexOf用于String中多个匹配的方法

There is a second parameter for indexOf which sets a start parameter. This code example prints all indices of x

indexOf有第二个参数,用于设置起始参数。此代码示例打印x的所有索引

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