find First Starts With - Java java.lang

Java examples for java.lang:String Start

Description

find First Starts With

Demo Code


//package com.java2s;
import java.util.Arrays;
import java.util.Collection;

public class Main {
    public static void main(String[] argv) {
        String text = "java2s.com";
        Collection starts = java.util.Arrays.asList("asdf", "java2s.com");
        System.out.println(findFirstStartsWith(text, starts));
    }/*from  w ww  .jav  a  2 s  .com*/

    public static String findFirstStartsWith(String text,
            Collection<String> starts) {
        String res = null;
        for (String start : starts) {
            if (text.startsWith(start)) {
                res = start;
                break;
            }
        }
        return res;
    }

    public static String findFirstStartsWith(String text, String[] starts) {
        return findFirstStartsWith(text, Arrays.asList(starts));
    }
}

Related Tutorials