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

Java examples for java.lang:String End

Description

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

Demo Code

//package com.java2s;

import java.util.Collection;

import java.util.Iterator;

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

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

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

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

Related Tutorials