2016-05-21 14 views
0

状況どのように文章で文字列の配列を見つけるのですか?

私は文字列の配列を持っています。 String[] arr = {"name","name2"};

私が欲しいものは?私は文がこれらwords.Java年代の一方または両方を持っている場合はtrueを返すように助けることができるJavaでの関数があるかどうかを知りたい

は メソッドは文字列だけではない配列を有することを可能にする「が含まれます」。

+0

あなたの文を単語に分割するには、java String.splitメソッドを使用します。そして、各単語を受け取り、containsを使用して配列内にあるかどうかを確認します。私はあなたのためにそれをやっていません。あなたの文を分割する方法は次のとおりです。http://www.tutorialspoint.com/java/java_string_split.htm – ThePerson

答えて

0

正規表現に変換すると、両方を同時に検索することができますが、単語内の文字列ではなく、正確な単語のみを検索することもできます。 firstname、はできません。

\b(?:name|name2)\b 

は、Java文字列リテラルとして、あなたは倍増する必要が \"\\b(?:name|name2)\\b"

単語のリストが動的である場合、あなたはその後、単語の任意の配列、そのようなないハードコードされた正規表現を、持っている、すなわち、正規表現を構築する際の言葉を引用することを忘れないでください:

StringJoiner joiner = new StringJoiner("|", "\\b(?:", ")\\b"); 
for (String word : words) 
    joiner.add(Pattern.quote(word)); 
Pattern p = Pattern.compile(joiner.toString()); 
Matcher m = p.matcher(sentence); 
if (m.find()) { 
    System.out.println("Sentence contained word: " + m.group()); 
} 

あなたもにifステートメントを変更することで、そのような単語のすべてのインスタンスを検索し、検索を続けることができます0ループ。

またcompile()コールにPattern.CASE_INSENSITIVEオプションを追加することができますので、それはあまりにもNameまたはNAMEと一致します。

1

あなたはこのコードを試すことができます。私はこの問題のためにjava.utilのStringTokenizerクラスを使用しました。コード内

外観:

public class StackOverFlowProblem { 


public static void prn(String message){ 
    System.out.println(message); 
} 

public static void main(String[] args) { 
    // TODO code application logic here 
    String [] list = {"Hello","My","Friends","How","Are","You"}; 
    String sentence = "Hey Hello"; 
    if(searchWordinString(sentence, list)) 
     prn("exists"); 
    else 
     prn("don't exists"); 
} 

public static boolean searchWordinString(String sentence,String[] list){ 
    StringTokenizer test = new StringTokenizer(sentence); 
    String element ="first iteration"; 
    while(!element.equals("")){ 
     try{ 
      element = test.nextElement().toString(); 
      prn(element); 
      for(int index=0;index<list.length;index++){ 
       if(element.equals(list[index])){ 
        prn(element+"="+list[index]); 
        return true; 
       } 
      } 
     } 
     catch(NoSuchElementException e){ 
      prn("Error: don't exist more words , bye"); 
      element=""; 
     } 
    } 
    return false; 
} 

} 

が、私は2つのパラメータを取る関数を作った、1センテンスであり、他の単語のリストです。あなたが単語のリストの任意の項目を見つけた場合、関数内

は、あなたの文章が文字列として格納されている場合

0

が、その後、単にアレイとチェックを反復そうでない場合はfalseにtrueを返します文の単語に一致しますcompare()を使用して、各単語のために:文を文字列配列として格納されている場合、あなたがしたい場合

private static boolean checkForWord(String sentence, String[] words) { 
    for (int i=0; i<words.length; i++) { 
     if (sentence.contains(words[i])) { 
      return true; 
     } 
    } 
    return false; //this statement can only be reached if there is no match 
} 

、あなたは、ネストされたforループを使用して両方の配列を反復処理し、equalsIgnoreCase()(または単にequals()を使用して単語を比較する必要があります大文字と小文字を区別する):

private static boolean checkForWord(String[] sentence, String[] words) { 
    for (int x=0; x<sentence.length; x++) { 
     for (int y=0; y<words.length; y++) { 
      if (sentence[x].equalsIgnoreCase(words[y])) { 
       return true; 
      } 
     } 
    } 
    return false; 
} 
関連する問題