Hutool 5.8.15: 查询字符串中所有匹配字符串的位置
在Hutool 5.8.15版本中,您可以使用StrUtil类的'indexOfAll'方法来查询字符串中包含一个字符串的所有位置。下面是示例代码:
import cn.hutool.core.util.StrUtil;
import java.util.List;
public class Main {
public static void main(String[] args) {
String str = 'hello world hello';
String searchStr = 'hello';
List<Integer> positions = StrUtil.indexOfAll(str, searchStr);
for (Integer position : positions) {
System.out.println('Found at position: ' + position);
}
}
}
输出结果将是:
Found at position: 0
Found at position: 12
这表示在字符串中找到了两个'hello'字符串,分别在位置0和位置12。
原文地址: http://www.cveoy.top/t/topic/pkze 著作权归作者所有。请勿转载和采集!