tests if a string starts with any one of a collection of prefixes - Java java.lang

Java examples for java.lang:String Start

Description

tests if a string starts with any one of a collection of prefixes

Demo Code

//package com.java2s;

import java.util.Collection;

import java.util.Iterator;

public class Main {
    /**/*from  w w  w.  j a va2s .c  o m*/
     * tests if a string starts with any one of a collection of prefixes
     */
    public static boolean startsWithAny(String str,
            Collection<String> prefixes) {
        if (str == null)
            return false;

        for (Iterator<String> iter = prefixes.iterator(); iter.hasNext();) {
            if (str.startsWith(iter.next()))
                return true;
        }
        return false;
    }

    /**
     * tests if a string starts with any one of a collection of prefixes
     */
    public static boolean startsWithAny(String str, String[] prefixes) {
        if (str == null)
            return false;

        for (int i = 0; i < prefixes.length; i++) {
            if (str.startsWith(prefixes[i]))
                return true;
        }
        return false;
    }
}

Related Tutorials