我正在尝试让它忽略这些情况,同时使用contains方法。我该怎么做?
String text = "Did you eat yet?";
if(text.contains("eat") && text.contains("yet"))
System.out.println("Yes");
else
System.out.println("No.");
答案 0 :(得分:1)
请使用
org.apache.commons.lang3.StringUtils.containsIgnoreCase("ABCDEFGHIJKLMNOP", "gHi");
答案 1 :(得分:1)
不幸的是String.containsIgnoreCase
没有String
方法。
但是,您可以使用正则表达式验证类似的条件。
例如:
String text = "Did you eat yet?";
// will match a String containing both words "eat",
// then "yet" in that order of appearance, case-insensitive
// | word boundary
// | | any character, zero or more times
Pattern p = Pattern.compile("\\beat\\b.*\\byet\\b", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(text);
System.out.println(m.find());
更简单的版本(感谢blgt):
// here we match the whole String, so we need start-of-input and
// end-of-input delimiters
// | case-insensitive flag
// | | beginning of input
// | | | end of input
System.out.println(text.matches("(?i)^.*\\beat\\b.*\\byet\\b.*$"));
<强>输出强>
true