Каков наилучший способ получить все позиции символов поисковой фразы для данной строки текста?
Например, скажем, у нас есть "the red cat is watching the red car stopped at the red stop sign"
"the red cat is watching the red car stopped at the red stop sign"
INPUT: "red"ВЫХОД: [5, 29, 52]
Вы можете использовать indexOf метод класса строки:
String haystack = "the red cat is watching the red car stopped at the red stop sign"; String needle = "red"; int idx = 0, pos; while( (pos = haystack.indexOf(needle,idx)) != -1) { System.out.println(pos+1); idx += pos+1; }
Смотри