慕碼人8056858
2019-10-30 14:43:42
我想檢查一個字符串是否按順序包含單詞“ 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
提交
取消