String match = "hello";
String text = "0123456789hello0123456789";
int position = getPosition(match, text); // should be 10, is there such a method?
原文由 hhh 发布,翻译遵循 CC BY-SA 4.0 许可协议
这适用于使用正则表达式。
String text = "I love you so much";
String wordToFind = "love";
Pattern word = Pattern.compile(wordToFind);
Matcher match = word.matcher(text);
while (match.find()) {
System.out.println("Found love at index "+ match.start() +" - "+ (match.end()-1));
}
输出 :
在索引 2 - 5 找到“爱”
一般规则 :
原文由 Aldwane Viegan 发布,翻译遵循 CC BY-SA 3.0 许可协议
8 回答6.4k 阅读
1 回答4.2k 阅读✓ 已解决
3 回答2.3k 阅读✓ 已解决
2 回答3.2k 阅读
2 回答3.9k 阅读
1 回答2.2k 阅读✓ 已解决
3 回答1.7k 阅读✓ 已解决
执行此操作的方法系列是:
int indexOf(String str)
indexOf(String str, int fromIndex)
int lastIndexOf(String str)
lastIndexOf(String str, int fromIndex)