我想使用通配符功能
如果pattern是* test *且string是 abctestbcd 我必须得到 start index = 3 , endIndex = 6
答案 0 :(得分:1)
试试这段代码:
String searchString = "test";
String s = "abctestbcd";
int startIndex = s.indexOf("test");
int endIndex = startIndex+searchString.length()-1;
System.out.println(startIndex);
System.out.println(endIndex);
如果可以进行多项匹配,请在循环中使用它:
String searchString = "test";
String s = "abctestbcdtest";
int startIndex = -1;
do {
startIndex = s.indexOf("test", startIndex+1);
int endIndex = startIndex+searchString.length()-1;
if (startIndex != -1){
System.out.println(startIndex);
System.out.println(endIndex);
}
} while(startIndex != -1);
答案 1 :(得分:0)
Pattern
可以帮到你。
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
// Check all occurrences
while (matcher.find()) {
System.out.print("Start index: " + matcher.start());
System.out.print(" End index: " + matcher.end());
System.out.println(" Found: " + matcher.group());
}
答案 2 :(得分:0)
使用subString()和indexOf()函数的组合。