给定一个字符串,判断该字符串中是否包含某个子串。如果包含,求出子串的所有出现位置。
如:"abcbcbabcb34bcbd"中,"bcb"子串的出现位置为: 1,3,7,12
public class WhereStrTest {
public static String WhereStr(String str, String wherestr) {
int index = 0;
String indexstr = "";
if (!str.contains(wherestr))
{
return "";
}
else
{
index = str.indexOf(wherestr, index);
indexstr += index;
while (str.substring(index+1).contains(wherestr))
{
index = str.indexOf(wherestr, index + 1);
indexstr += "," + index;
}
return indexstr;
}
}
public static void main(String[] args) {
System.out.println(WhereStr("abcbcbabcb34bcbd", "bcb"));
}
}