Java 是否使用.contains方法忽略大小写的选项?
是否有使用Java 是否使用.contains方法忽略大小写的选项?,java,list,arraylist,Java,List,Arraylist,是否有使用.contains()方法忽略大小写的选项 我有一个DVD对象的ArrayList。每个DVD对象都有几个元素,其中一个是标题。我有一个搜索特定标题的方法。它可以工作,但我希望它不区分大小写 您不能保证总是将String对象取回,或者您在列表中使用的对象实现了忽略大小写的方法 如果要将集合中的Strings与独立于大小写的内容进行比较,则需要迭代集合,并在不使用大小写的情况下进行比较 String word = "Some word"; List<String> aList
.contains()
方法忽略大小写的选项
我有一个DVD对象的
ArrayList
。每个DVD对象都有几个元素,其中一个是标题。我有一个搜索特定标题的方法。它可以工作,但我希望它不区分大小写 您不能保证总是将String
对象取回,或者您在列表中使用的对象实现了忽略大小写的方法
如果要将集合中的String
s与独立于大小写的内容进行比较,则需要迭代集合,并在不使用大小写的情况下进行比较
String word = "Some word";
List<String> aList = new ArrayList<>(); // presume that the list is populated
for(String item : aList) {
if(word.equalsIgnoreCase(item)) {
// operation upon successful match
}
}
String word=“Some word”;
列表列表=新的ArrayList();//假设列表已填充
for(字符串项:aList){
if(字等信号情况(项目)){
//比赛成功后进行手术
}
}
我猜您的意思是在字符串中搜索时忽略大小写
我不知道,但您可以尝试将要搜索的字符串转换为小写或大写,然后进行搜索
// s is the String to search into, and seq the sequence you are searching for.
bool doesContain = s.toLowerCase().contains(seq);
编辑:
正如Ryan Schipper所建议的,根据您的情况,您也可以(而且可能会更好)执行seq.toLowerCase()。这可能不是解决特定问题的最佳方法,但您可以使用String.matches(String regex)
方法或matcher等效方法。我们只需要根据您的预期标题构造一个正则表达式。这就变得复杂了
List<DVD> matchingDvds(String titleFragment) {
String escapedFragment = Pattern.quote(titleFragment);
// The pattern may have contained an asterisk, dollar sign, etc.
// For example, M*A*S*H, directed by Robert Altman.
Pattern pat = Pattern.compile(escapedFragment, Pattern.CASE_INSENSITIVE);
List<DVD> foundDvds = new ArrayList<>();
for (DVD dvd: catalog) {
Matcher m = pat.matcher(dvd.getTitle());
if (m.find()) {
foundDvds.add(dvd);
}
}
return foundDvds;
}
但这会将模式编译太多次。下套管怎么样
if (dvd.getTitle().toLowerCase().contains(titleFragment.toLowerCase()))
祝贺你;你刚刚发现了土耳其的问题。除非在toLowerCase
中声明区域设置,否则Java将查找当前区域设置。下框速度较慢,因为它必须考虑土耳其无点i和点i。至少您没有模式和匹配项。私有列表查找字符串(String stringToLookFor,List arrayToSearchIn)
private List<String> FindString(String stringToLookFor, List<String> arrayToSearchIn)
{
List<String> ReceptacleOfWordsFound = new ArrayList<String>();
if(!arrayToSearchIn.isEmpty())
{
for(String lCurrentString : arrayToSearchIn)
{
if(lCurrentString.toUpperCase().contains(stringToLookFor.toUpperCase())
ReceptacleOfWordsFound.add(lCurrentString);
}
}
return ReceptacleOfWordsFound;
}
{
List ReceptacleOfWordsFound=new ArrayList();
如果(!arrayToSearchIn.isEmpty())
{
for(字符串lCurrentString:arrayToSearchIn)
{
if(lCurrentString.toUpperCase().contains(stringToLookFor.toUpperCase())
add(lCurrentString);
}
}
沃兹基金会的返回接收器;
}
私有布尔containsSignoreCase(列表,字符串soughtFor){
用于(当前字符串:列表){
if(当前等信号情况(soughtFor)){
返回true;
}
}
返回false;
}
将两个操作数转换为小写(或大写)的直观解决方案的效果是为每个项实例化一个额外的字符串对象,这对于大型集合来说是无效的。此外,正则表达式比简单的字符比较慢一个数量级
String.regionMatches()
允许以不区分大小写的方式比较两个字符串区域。使用它,可以编写一个高效版本的不区分大小写的“contains”方法。我使用以下方法;它基于Apache commons lang的代码:
public static boolean containsIgnoreCase(final String str, final String searchStr) {
if (str == null || searchStr == null) {
return false;
}
final int len = searchStr.length();
final int max = str.length() - len;
for (int i = 0; i <= max; i++) {
if (str.regionMatches(true, i, searchStr, 0, len)) {
return true;
}
}
return false;
}
public静态布尔containsSignoreCase(final String str,final String searchStr){
if(str==null | | searchStr==null){
返回false;
}
final int len=searchStr.length();
最终整数最大值=长度();
对于(inti=0;i,你可以在这上面应用小技巧。
将所有字符串更改为同一大小写:大写或小写
对于C代码:
import java.util.*;
class Test
{
public static void main(String[] args)
{
String itemCheck="check";
ArrayList<String> listItem =new ArrayList<String>();
listItem.add("Check");
listItem.add("check");
listItem.add("CHeck");
listItem.add("Make");
listItem.add("CHecK");
for(String item :listItem)
{
if(item.toUpperCase().equals(itemCheck.toUpperCase()))
{
System.out.println(item);
}
}
}
}
列出searchResults=sl.FindAll(s=>s.ToUpper().Contains(seachKeyword.ToUpper())
对于Java代码:
import java.util.*;
class Test
{
public static void main(String[] args)
{
String itemCheck="check";
ArrayList<String> listItem =new ArrayList<String>();
listItem.add("Check");
listItem.add("check");
listItem.add("CHeck");
listItem.add("Make");
listItem.add("CHecK");
for(String item :listItem)
{
if(item.toUpperCase().equals(itemCheck.toUpperCase()))
{
System.out.println(item);
}
}
}
}
import java.util.*;
课堂测试
{
公共静态void main(字符串[]args)
{
String itemCheck=“检查”;
ArrayList listItem=新的ArrayList();
列表项。添加(“检查”);
列表项。添加(“检查”);
列表项。添加(“检查”);
列表项。添加(“制造”);
列表项。添加(“检查”);
for(字符串项:listItem)
{
if(item.toUpperCase().equals(itemCheck.toUpperCase()))
{
系统输出打印项次(项);
}
}
}
}
如果您使用的是Java 8
List<String> list = new ArrayList<>();
boolean containsSearchStr = list.stream().anyMatch("search_value"::equalsIgnoreCase);
List List=new ArrayList();
布尔containsSearchStr=list.stream().anyMatch(“搜索值”::equalsIgnoreCase);
在Java 8中,您可以使用流接口:
return dvdList.stream().anyMatch(d -> d.getTitle().equalsIgnoreCase("SomeTitle"));
对于Java8,您还可以有一个如下所示的解决方案
List<String> list = new ArrayList<>();
String searchTerm = "dvd";
if(String.join(",", list).toLowerCase().contains(searchTerm)) {
System.out.println("Element Present!");
}
List List=new ArrayList();
字符串searchTerm=“dvd”;
if(String.join(“,”,list).toLowerCase()包含(searchTerm)){
System.out.println(“元素存在!”);
}
如果您正在寻找包含和不等于,那么我将提出以下解决方案。
唯一的缺点是,若您在下面的解决方案中的搜索项是“DE”,那个么它也会匹配
List<String> list = new ArrayList<>();
public static final String[] LIST_OF_ELEMENTS = { "ABC", "DEF","GHI" };
String searchItem= "def";
if(String.join(",", LIST_OF_ELEMENTS).contains(searchItem.toUpperCase())) {
System.out.println("found element");
break;
}
List List=new ArrayList();
公共静态最终字符串[]元素列表={“ABC”、“DEF”、“GHI”};
字符串searchItem=“def”;
if(String.join(“,”,元素列表)。包含(searchItem.toUpperCase()){
System.out.println(“找到的元素”);
打破
}
在DVD列表和您的搜索字符串上进行空检查
if (!StringUtils.isEmpty(searchString)) {
return Optional.ofNullable(dvdList)
.map(Collection::stream)
.orElse(Stream.empty())
.anyMatch(dvd >searchString.equalsIgnoreCase(dvd.getTitle()));
}
我知道我参加聚会有点晚,但在科特林,你可以很容易地使用:
fun Collection<String>.containsIgnoreCase(item: String) = any {
it.equals(item, ignoreCase = true)
}
val list = listOf("Banana")
println(list.contains("banana"))
println(list.containsIgnoreCase("BaNaNa"))
fun Collection.containsSignoreCase(项目:字符串)=任意{
it.equals(item,ignoreCase=true)
}
val list=listOf(“香蕉”)
println(list.contains(“香蕉”))
println(列表.containsIgnoreCase(“香蕉”))
Kotlin开发人员,与/
私人娱乐比较分类(
类别:列表?,
类别:字符串
)=categories?.any{it.equals(categories,true)}?:false
不,没有。但如果您解释了您要做的事情,我们可能会建议一种替代方法来满足您的需要。执行案例
fun Collection<String>.containsIgnoreCase(item: String) = any {
it.equals(item, ignoreCase = true)
}
val list = listOf("Banana")
println(list.contains("banana"))
println(list.containsIgnoreCase("BaNaNa"))
private fun compareCategory(
categories: List<String>?,
category: String
) = categories?.any { it.equals(category, true) } ?: false