繁体   English   中英

如何在字符串中搜索关键字

[英]how to search for keywords in strings

我需要做一些关键字搜索并打印(如果为true)。 如果我按顺序进行比较,效果很好。 但是我要

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

做一些java编程= true

一些java = true

做编程= true

最后最重要的是

编程java = true

Java编程有些做= 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
        }
        }
    }
}

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

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

对于每个输入字符串,请使用split()将其拆分为单词,然后创建任何种类的集合( Set<String>将起作用,但是通过使用Arrays.asList创建List<String>也可以。

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

(请注意,您必须首先确保输入字符串不是空集。此外,如果输入字符串中有多个单词出现多次,那么这种方法就不会在没有其他逻辑的情况下捕获到该字符串。)

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

 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; } } 

为此,Regex将成为您的朋友。

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

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;
     }      
  }
}

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

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

String[] given = new String[3];

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

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

在这里阅读更多关于它的信息

其次 ,在您的if语句中,您要检查输入是否包含给定的array元素

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");
        }
    }

输出

在此处输入图片说明

假设其他条件都很好,请进行以下修改

//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数组中使用它。

暂无
暂无

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

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