我想检查一个字符串是否按顺序包含单词“ stores”,“ store”和“ product”。无论它们之间是什么。我尝试使用someString.contains(stores%store%product);并且.contains("stores%store%product");我是否需要显式声明一个正则表达式并将其传递给方法,还是根本无法传递正则表达式?
3 回答
潇潇雨雨
TA贡献1833条经验 获得超4个赞
public static void main(String[] args) {
String test = "something hear - to - find some to or tows";
System.out.println("1.result: " + contains("- to -( \\w+) som", test, null));
System.out.println("2.result: " + contains("- to -( \\w+) som", test, 5));
}
static boolean contains(String pattern, String text, Integer fromIndex){
if(fromIndex != null && fromIndex < text.length())
return Pattern.compile(pattern).matcher(text).find();
return Pattern.compile(pattern).matcher(text).find();
}
1.结果:正确
2.结果:正确
添加回答
举报
0/150
提交
取消