String starts With Any Iterable - Java Collection Framework

Java examples for Collection Framework:Iterable

Description

String starts With Any Iterable

Demo Code


//package com.java2s;

public class Main {
    /**/*w ww .  j a v a  2  s. c  om*/
     * @param stringList iterable of possible starting words
     * @param str String to be examined
     * @return true if str starts with any of the Strings in startArray, false otherwise.
     */
    public static boolean startsWithAny(Iterable<String> stringList,
            String str) {
        return getStartingIndex(stringList, str) != -1;
    }

    /**
     * @param stringList iterable of possible starting words
     * @param examinedString String to be examined 
     * @return index of first matching element in startArray if matched, -1 otherwise
     */
    public static int getStartingIndex(Iterable<String> stringList,
            String examinedString) {
        int i = 0;
        for (String string : stringList) {
            if (examinedString.startsWith(string)) {
                return i;
            }
            i++;
        }
        return -1;
    }
}

Related Tutorials