首页 > 解决方案 > 如何使用 Contains 忽略大小写

问题描述

我有 2 个字符串,想比较忽略大小写。

Ex: 
s1.contains(s2.toLowerCase()) in this case the problem is s1 might be in upper case. 
s1.toLowerCase().contains(s2) in this case the problem is s2  might be in upper case. 

唯一的选择是我需要将两个字符串都更改为小写以查找包含。

ex: s1.toLowerCase().contains(s2.toLowerCase())

是否有任何实用程序或字符串方法而不是使用这种方法,因为 String 是不可变的。

谢谢。

标签: java

解决方案


您可以使用来自 apache 的 StringUtils。

StringUtils.containsIgnoreCase(str, searchStr)

https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html


推荐阅读