java - 如何使用 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 是不可变的。
谢谢。
解决方案
您可以使用来自 apache 的 StringUtils。
StringUtils.containsIgnoreCase(str, searchStr)
https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html
推荐阅读
- scala - 隐式和召唤有什么区别?
- sql - 使用 case 时显示一种结果
- r - 如何替换R中列中的值名称
- pytorch - 使用 Emotic 演示时 pytorch.loads() 函数中的第一个参数错误
- linux - 由于 SSL/TLS 无法连接到 SQL Server
- typescript - 如何在 TypeORM 上保存与自定义实体(表)的多对多关系?
- google-cloud-platform - 谷歌云平台数据集无法在 VertexAI 中显示图像
- javascript - 如何在 React 中获得 key prop 警告的同时向我的组件添加密钥
- r - 尝试创建一个函数,当将数据帧输入其中时,将某些字符串的名称替换为不同的字符串
- python - 在不使用 Numpy 和 Pandas 的情况下从列表中计算平均值?