简体   繁体   English

我怎么知道给定字符串的一部分是否是Arraylist中的字符串 <String> ?

[英]How do I know if part of a given string is a string in a Arraylist<String>?

I'm doing a homework assignment and I'm having a hard time trying to find out how to figure out if part of a given String is a string in an Arraylist of String. 我正在做作业,很难找到如何弄清楚给定String的一部分是否为String Arraylist的字符串。

Example of given string: 给定字符串的示例:

loveaspd
asdhouseaspd

arraylist of string: love, house, home, dog, amuse, problem... 字符串数组:爱,房子,家,狗,娱乐,问题...

I have tried to use contains but just realized it will not help me once I have to check each letter. 我曾尝试使用contains,但只是意识到一旦我必须检查每个字母,它就无济于事。

Example: loveaspr 示例: loveaspr

my program is supposed to check the word love , so it will see if the first letter matches and go to the second letter. 我的程序应该检查“ love ”一词,因此它将查看第一个字母是否匹配,然后转到第二个字母。 So, after reading the word love , the output will be: 因此,在阅读单词love ,输出将是:

love exists.

The the program will check the letter a . 程序将检查字母a There is a word with the letter a in my arrayList so it will check the second letter and will see that there is no such word where the first letter is a and the second is s . 有字母一个字a在我的ArrayList所以它会检查的第二个字母,并会看到,有没有这样的字,其中第一个字母是a ,第二个是s then a will be rejected and s will be rejected. 那么a将被拒绝, s将被拒绝。

The last part of the string will be checked (note that I have the word problem in my arrayList, so the program will check the first and second letter and then both will be rejected together). 字符串的最后一部分将被检查(请注意,我的arrayList中有单词问题,因此程序将检查第一个和第二个字母,然后一起将其拒绝)。

Therefore, my output should be: 因此,我的输出应为:

love exists.
a rejected.
s rejected.
pr rejected.

The problem is that I don't know how to check each letter in each word of my ArrayList. 问题是我不知道如何检查ArrayList的每个单词中的每个字母。 I'm not asking for someone to code my homework for me. 我不是在要求别人为我编写作业代码。 I'm looking for someone who could say which topic I should study (specifically) or give an example of something related to my issue in order to complete the task (it is due tomorrow). 我正在寻找可以说我应该(专门)研究哪个主题或给出与我的问题相关的示例以完成任务的人(该任务将于明天到期)。

Thanks in advance, 提前致谢,

Ian Pierre. 伊恩·皮埃尔(Ian Pierre)。

Go on like this 这样下去

ArrayList<String> ar = new ArrayList<String>();
        for (String a : ar) {
            if (a.contentEquals("any_string_here") || a.equalsIgnoreCase("any_string_here")) {
                System.out.println("Found");
            }
        }

One option is to iterate the list, and use String.indexOf() to determine partial matches. 一种选择是迭代列表,并使用String.indexOf()确定部分匹配项。 Here is an example: 这是一个例子:

  private static String match(List<String> mots, String searchPhrase) {
        for(String mot : mots) {
            if(searchPhrase.indexOf(mot) >= 0) {
                return mot;
            }
        }
        return "No Match Found.";
    }

    //Test
    public static void main(String[] args) {
        List<String> mots = new ArrayList<>();
        mots.add("un");
        mots.add("deux");
        mots.add("trois");
        String searchPhrase = "lesdeuxchats";
        System.out.println( match(mots,searchPhrase) );  //Output = "deux"
    } 
String s;
ArrayList<String> list= new ArrayList<String>();
int i,j;

for(i=0;i<s.length;i++)
{
    for(j=i+1;j<s.length;j++){
        if(list.contains(s.substring(i,j)){
            System.out.println("The list contains "+s.substring(i,j));
        }
    }
}

Substrings ( http://www.tutorialspoint.com/java/java_string_substring.htm ) are the way to get parts of your original string. 子字符串( http://www.tutorialspoint.com/java/java_string_substring.htm )是获取原始字符串部分的方法。

public class SO {       
    public static void main(String[] args) {
        ArrayList<String> al = new ArrayList<String>();
        al.add("ABC");
        al.add("DEF");
        al.add("GHI");
        al.add("JKL");
        al.add("MNO");

        for (String arrList : al) {
            if(arrList == "GHI"){
                System.out.println("Found");
            }
        }
        }       
    }

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

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