简体   繁体   English

将字符串与arraylist中的一部分字符串进行比较

[英]Comparing a string to a part of string inside an arraylist

The title may be a little confusing or misleading but I'm trying to compare a single string: 标题可能有点混乱或误导,但我正在尝试比较单个字符串:

String mystring = "abc";

And then try to compare it with every single string contained in an arraylist 然后尝试将其与arraylist中包含的每个单个字符串进行比较

ArrayList<String> array = new ArrayList<String>();
array.add("The first letters of the alphabet are abc");
array.add("abc");
array.add("Alphabet");
array.add("Nothing");

But (and I'm not certain) it seems that contains is true only if both strings matches and not if the word or phrase is inside the string. 但是(而且我不确定),似乎只有当两个字符串都匹配时,contains才是正确的,而不是单词或短语在字符串中时才包含。

How can I compare a desired string to a "phrase" that is within an ArrayList of strings? 如何将所需的字符串与字符串ArrayList中的“短语”进行比较?

To check what you're asking for, you would have to iterate through the values that are contained in the array, and check whether they contain the value of the string that you want to check. 要检查您要的内容,您必须遍历数组中包含的值,并检查它们是否包含要检查的字符串的值。

for (String fromArray : array) {
    if (fromArray.contains(mystring)) {
        // put your code here
    }
}

This method is case sensitive . 此方法区分大小写 To overcome this, you can either change the strings to lowercase (with the toLowerCase() method), or use a library that has been designed to do so. 为了克服这个问题,您可以将字符串更改为小写(使用toLowerCase()方法),也可以使用为此目的设计的库。 One such library is the Apache Commons library. 这样的库之一就是Apache Commons库。

http://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/StringUtils.html#containsIgnoreCase%28java.lang.CharSequence,%20java.lang.CharSequence%29 http://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/StringUtils.html#containsIgnoreCase%28java.lang.CharSequence,%20java.lang.CharSequence%29

If you want to check whether the array element, instead of containing, instead matches the string you want to check it against, you would use the matches() method. 如果要检查数组元素(而不是包含数组)是否匹配要检查的字符串,则可以使用matches()方法。

Documentation links: 文档链接:

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

Apache commons StringUtils Apache Commons StringUtils

try this 尝试这个

// check if string is exist in array
if(array.Contains("YourString"))
{
// if contain then u can use this or do operations
Response.Write("YourString is available in array");
}

this will help you 这会帮助你

It's simple. 这很简单。

You use an Iterator over the ArrayList. 您在ArrayList上使用迭代器。

for(String s: array):
  if(s.toLowerCase().contains(myString.toLowerCase())) { //find if such a substring exits within each string
      //do something
    }
}

If however you don't want to check case sensitive-ness you get remove the toLowerCase() from the lines. 但是,如果您不想检查是否区分大小写,则可以从行中删除toLowerCase()。

I hope you are aware of what toLowerCase() does and putting it in the code doesn't makes a difference for you. 我希望您知道toLowerCase()的作用,并将其放入代码中对您没有影响。

You need iterate through all the elements of Array List and then need to check using contains() method 您需要遍历数组列表的所有元素,然后需要使用contains()方法进行检查

for (String str : array){
            if(str.contains(mystring)){
                System.out.println(str + " Conatins" + mystring);
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM