【问题标题】:how to search for keywords in strings如何在字符串中搜索关键字
【发布时间】:2014-12-08 07:38:10
【问题描述】:

如果属实,我需要进行一些关键字搜索和打印。 如果我按顺序比较,效果很好。但我想

比较以下情况并期望它们是正确的。

做一些java编程=真

一些java = true

做编程 = true

最后是最重要的

编程 java = true

编程 java some do = true

对于上面提到的所有情况,我都需要返回 true,但到目前为止它只适用于情况 1 和 2

public class Examples {

    public static void main(String[] args) {
        String[] given = new String[20];
        given[0]  = ("do some java programming");
        given[1] = ("do some grocery shopping");
        given[2] = ("play soccer at the west field");
        String input = new String();
        Scanner userInput = new Scanner(System.in);

        System.out.println("Enter the string to compare");
        input[0] = userInput.nextLine();
        for (int i=0; i <20; i++){
        if(given[i].contains(input))
        {
            System.out.println(given[i]);
        }
        else
        {
            //do nothing
        }
        }
    }
}

【问题讨论】:

  • 您是否愿意接受比您的更清洁的其他解决方案?像 swtich 语句
  • 是的,我也需要编辑我的输入。它是字符串而不是字符串数组。

标签: java search compare contains matcher


【解决方案1】:

因为这个正则表达式将成为你的朋友。

这里有一些可以使用的工作代码:

String[] matches = input[0].split(" ");           
for (int i=0; i <3; i++){
  for(String s: matches){
    if(given[i].contains(s))
      System.out.println(given[i]);
      break;
     }      
  }
}

【讨论】:

  • 非常感谢您的意见!这真的帮助我更容易地得到我想要的东西。另外感谢其他所有人的投入,他们也很有帮助
  • 很高兴为您提供帮助,欢迎来到 StackOverflow!不要忘记为好的答案投票,并验证您认为最有帮助的答案:)
【解决方案2】:

您的代码几乎是正确的,但需要进行一些更改

首先,因为在您的示例代码中有3 的情况,最好定义您给定的数组长度3

String[] given = new String[3];

注意:对于更多的情况,你可以定义更大的数组长度;例如,如果您要添加其他 2 种情况,则您的数组长度为 5

对于所有引用类型,如果数组长度超过您的需要,则默认值为 null。

read more about it here

第二,在你的if语句中,你要检查输入是否包含给定的数组元素

if (input.contains(given[i])) {

代码

    String[] given = new String[3];
    given[0] = ("do some java programming");
    given[1] = ("do some grocery shopping");
    given[2] = ("play soccer at the west field");

    Scanner userInput = new Scanner(System.in);

    System.out.println("Enter the string to compare");
    String input = userInput.nextLine();

    for (int i = 0; i < given.length; i++) {
        if (input.contains(given[i])) {
            System.out.println(given[i]);
        } else {
          //  System.out.println("do nothing");
        }
    }

输出

【讨论】:

    【解决方案3】:

    拆分给定的行并将其存储在列表中。
    再次拆分输入行并逐字比较。
    下面是代码sn-p

    public class StringCompare
    {
        public static final String delimiter = " ";
    
        public static void main( String[] args )
        {
            String[] given = new String[20];
            given[ 0 ] = ( "do some java programming" );
            given[ 1 ] = ( "do some grocery shopping" );
            given[ 2 ] = ( "play soccer at the west field" );
    
            List< List< String >> listLineAsWords = new ArrayList< List< String >>();
            
            //split each line and store it as list.
            for ( String line : given )
            {
                if ( line == null )
                    break;
                listLineAsWords.add( Arrays.asList( line.split( delimiter ) ) );
            }
    
            
            //Write your own logic to get the input line
            String inputLine = "programming java";
            if ( compareLine( inputLine, listLineAsWords ) )
                System.out.println( "The input line is part of given lines" );
        }
    
        private static boolean compareLine( String inputLine, List< List< String >> listLineAsWords )
        {
            if ( inputLine == null )
                return false;
            List< String > words = Arrays.asList( inputLine.split( delimiter ) );
            for ( List< String > listOfWords : listLineAsWords )
            {
                boolean isPartOfLine = true;
                for ( String word : words )
                {
                    if ( !listOfWords.contains( word ) )
                    {
                        isPartOfLine = false;
                        break;
                    }
                }
                if(isPartOfLine)
                    return true;
            }
            return false;
        }
    }

    【讨论】:

    • 我刚刚开始为 StackOverFlow 贡献代码,所以点赞会很棒。 @Omer
    【解决方案4】:

    解决此问题的一种方法的概述:

    given 中的每个字符串都应转换为 Set&lt;String&gt;,它是字符串中所有单词的集合。在每个字符串上使用split() 来获取单词,然后遍历单词列表并将每个单词添加到Set

    对于每个输入字符串,使用split() 将其拆分为单词,然后创建任何类型的集合(Set&lt;String&gt; 可以,但使用Arrays.asList 创建List&lt;String&gt; 也可以。

    然后,您可以使用SetcontainsAll 方法查看输入中的单词集合是否是每个given 字符串中单词集合的子集。

    (请注意,您必须首先确保输入字符串不是空集。此外,如果输入字符串中的任何单词多次出现,这种方法将无法捕捉到这一点,没有一些额外的逻辑。)

    【讨论】:

    • 为什么不用 Switch 语句会更干净?
    • @ajb 我不知道转换给定的集合是否适用于对象数组列表。我期待一个更大的任务,其中我将对象存储在 arrayList 中并搜索作为类的即时变量的 title 并打印与 title 关键字匹配的所有对象。
    • @Omer 我在想你需要一个ArrayList&lt;Set&lt;String&gt;&gt;,其中ArrayList 的大小为3(在你的例子中),每个元素都是一个字符串中的一组关键字。我不确定我是否了解您未来的要求。无论如何,我希望答案中的至少一些想法会有所帮助。
    • @ajb 我认为这给了我一个入门的好主意。非常感谢
    【解决方案5】:

    假设其他一切正常,如下修改

    //here initialize 'given' array
    //get the user input in a string that is 'input[0]' in your case
    int count = 0; 
    for (String s : given) {
       if (s != null && s.matches(input[0])) {
          //it matches and print it
       } else {
          //either it's not a match or the 'given' array has a null at this index
       }
       count++;
    }
    

    我必须说,以字符串形式获取用户输入。我不明白你为什么把它放在input 数组中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-13
      • 2019-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多