简体   繁体   English

如何正确检查一个字符串是否包含来自字符串数组的另一个字符串? -JAVA

[英]How to properly check if a string contains another string from a string array? - JAVA

I was trying to get program working and it runs and all but it seems to believe that every string contains words from a string array. 我试图使程序正常运行,并且可以运行,但似乎所有的字符串都包含字符串数组中的单词。 I am using the openCSV library to try and go through a csv file that contains business names and people names in the same column and I am trying to make it so that all company names would appear as the second column and all people names would appear on a third column. 我正在使用openCSV库尝试遍历包含同一列中的公司名称和人员名称的csv文件,并且尝试使所有公司名称显示为第二列,并且所有人员名称显示在第三栏。 The first column is just an identifying number. 第一列只是一个识别号。

for (String[] row : inputEntries)
    {
        for(int i = 0; i < dictionary.length; i++)
        {
            String rowEntry = row[1].toLowerCase();
            String dictionaryTerm = dictionary[i].toLowerCase();

            if(rowEntry.contains(dictionaryTerm))
            {
                String entries = row[0] + "," + row[1] + "," + "";
                String[] output = entries.split(",");
                writer.writeNext(output);
                System.out.println(output + ": This contained a Dictionary word");
                break;
            }
            else if (i == dictionary.length)
            {
                String entries = row[0] + "," + "" + "," + row[1];
                String[] output = entries.split(",");
                writer.writeNext(output);
                System.out.println(output + ": This did not contain a Dictionary word");
                break;
            }
        }
    }

It does output to a separate csv which is intended but it seems to think that everything is a business name so I get a file that looks identical to the original. 它确实将输出输出到一个单独的csv,但它似乎认为所有内容都是公司名称,因此我得到的文件看上去与原始文件相同。 What is a possible solution to this dilemma? 解决这一难题的可能方法是什么? Am I misusing the contains function? 我是否在滥用contains函数?

Input 输入

"11111111","John Smith"
"11111112","Wells Fargo Bank"
"11111113","Company name LLC"
"11111114","John Connor"

Output 产量

"11111111","","John Smith"
"11111112","Wells Fargo Bank",""    
"11111113","Company name LLC",""    
"11111114","","John Connor"

So working a little more on it I was able to get it to sorta do what I wanted but the problem is that it seems to only be checking for the first term in the dictionary string. 因此,我对其进行了更多的工作,使它能够执行我想要的操作,但问题是它似乎仅在检查字典字符串中的第一个术语。 Here's the updated code: 这是更新的代码:

    boolean match = false;
    boolean nomatch = false;
    int dicLength = dictionary.length;

    for (String[] row : inputEntries)
    {
        for(int i = 0; i < dicLength; i++)
        {
            String rowEntry = row[1].toLowerCase();
            String dictionaryTerm = dictionary[i].toLowerCase();

            match = rowEntry.contains(dictionaryTerm);

            if(match == true)
            {
                String entries = row[0] + "," + row[1] + "," + "";
                String[] output = entries.split(",");
                writer.writeNext(output);
                System.out.println(output + ": This contained a Dictionary word");
                match = false;
                break;
            }

            if (i == (dicLength - 1))
            {
                nomatch = true;
            }

            if (nomatch == true)
            {
                String entries = row[0] + "," + "" + "," + row[1];
                String[] output = entries.split(",");
                writer.writeNext(output);
                System.out.println(output + ": This did not contain a Dictionary word");
                match = false;
                break;
            }
        }
    }

Looking at your updated code, you are accidentally setting match = false rather than nomatch = false when you check nomatch == true. 查看更新的代码,当您检查nomatch == true时,您不小心将match = false设置为nomatch = false。 The best way to solve this would be to eliminate those variables completely and compact it to the following (also that break at the end isn't needed as for the loop is about to close): 解决此问题的最佳方法是完全消除这些变量并将其压缩为以下变量(并且由于循环即将结束,因此不需要在结尾处中断):

    int dicLength = dictionary.length;

    for (String[] row : inputEntries) {
        for(int i = 0; i < dicLength; i++) {

            String rowEntry = row[1].toLowerCase();
            String dictionaryTerm = dictionary[i].toLowerCase();

            if(rowEntry.contains(dictionaryTerm)) {
                String entries = row[0] + "," + row[1] + "," + "";
                String[] output = entries.split(",");
                writer.writeNext(output);
                System.out.println(output + ": This contained a Dictionary word");
                break;
            }

            if (i == (dicLength - 1)) {
                String entries = row[0] + "," + "" + "," + row[1];
                String[] output = entries.split(",");
                writer.writeNext(output);
                System.out.println(output + ": This did not contain a Dictionary word");
            }
        }
    }

You can make this even more efficient by not checking your dic length every iteration, (force it outside of the loop) but that's up to you! 您不必每次迭代都检查自己的dic长度(将其强制放在循环外),从而提高效率,这取决于您! :) :)

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

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