Determines if a string is in a list of phrases - Java java.lang

Java examples for java.lang:String Contain

Description

Determines if a string is in a list of phrases

Demo Code


//package com.java2s;

import java.util.regex.Pattern;

public class Main {
    /**/* ww  w  . ja  v a  2s.c om*/
     * Determines if a string is in a list of phrases
     * @param string String of word(s) that may or may not be in phrases
     * @param phrases iterable of phrases to be examined
     * @param caseSensitive true if string is matched case sensitively, false otherwise
     * @return true if any words are in any phrase, false otherwise
     */
    public static boolean stringInPhrases(String string,
            Iterable<String> phrases, Boolean caseSensitive) {

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

        return false;
    }

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

Related Tutorials