我有两个 String
s, str1
和 str2
。如何检查 str2
是否包含在 str1
中,忽略大小写?
原文由 trinity 发布,翻译遵循 CC BY-SA 4.0 许可协议
matches()
怎么样?
String string = "Madam, I am Adam";
// Starts with
boolean b = string.startsWith("Mad"); // true
// Ends with
b = string.endsWith("dam"); // true
// Anywhere
b = string.indexOf("I am") >= 0; // true
// To ignore case, regular expressions must be used
// Starts with
b = string.matches("(?i)mad.*");
// Ends with
b = string.matches("(?i).*adam");
// Anywhere
b = string.matches("(?i).*i am.*");
原文由 Jim Raynor 发布,翻译遵循 CC BY-SA 3.0 许可协议
8 回答6.5k 阅读
4 回答684 阅读✓ 已解决
2 回答3.4k 阅读
3 回答1.9k 阅读✓ 已解决
1 回答2.1k 阅读✓ 已解决
1 回答2.1k 阅读✓ 已解决
1 回答959 阅读✓ 已解决
更新:
原始答案是使用
toLowerCase()
方法。但正如某些人正确注意到的那样,Unicode 中存在一些例外情况,最好使用toUpperCase()
。因为: