Determines if any string in a list are in a given phrase - Java java.lang

Java examples for java.lang:String Contain

Description

Determines if any string in a list are in a given phrase

Demo Code


//package com.java2s;

import java.util.regex.Pattern;

public class Main {
    /**/*from w w w  . jav a2s. co  m*/
     * Determines if any string in a list are in a given phrase
     * @param strings String of word(s) that may or may not be in phrases
     * @param phrase iterable of phrases to be examined
     * @param caseSensitive true if strings are matched case sensitively, false otherwise
     * @return true if any words are in any phrase, false otherwise
     */
    public static boolean stringsInPhrase(Iterable<String> strings,
            String phrase, Boolean caseSensitive) {

        for (String string : strings) {
            string = Pattern.quote(string);
            Pattern pattern = caseSensitive ? Pattern.compile(string)
                    : Pattern.compile(string, Pattern.CASE_INSENSITIVE
                            | Pattern.UNICODE_CASE);
            if (pattern.matcher(phrase).find()) {
                return true;
            }
        }

        return false;

    }

    /**
     * Determines if any string in a list are in a given phrase, case insensitively
     * @param strings String of word(s) that may or may not be in phrases
     * @param phrase iterable of phrases to be examined
     * @return true if any words are in any phrase, case insensitively, false otherwise
     */
    public static boolean stringsInPhrase(Iterable<String> strings,
            String phrase) {
        return stringsInPhrase(strings, phrase, false);
    }
}

Related Tutorials