Gets the index of the first occurrence of the given regex from a String List. - Android java.util

Android examples for java.util:List

Description

Gets the index of the first occurrence of the given regex from a String List.

Demo Code

/* This file is part of the InternalPluginManager.
 *
 * Copyright (C) 2014-2015 Fabian Damken
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version./*w  ww  .j a v  a 2  s.  c om*/
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
//package com.book2s;

import java.util.List;

public class Main {
    public static void main(String[] argv) {
        String regex = "book2s.com";
        List list = java.util.Arrays.asList("asdf", "book2s.com");
        System.out.println(indexOf(regex, list));
    }

    /**
     * Gets the index of the first occurrence of the given regex.
     *
     * @param regex
     *            The regex to search for.
     * @param list
     *            The list that may contains the given regex.
     * @return The index of the first occurrence. If not found, <code>-1</code>.
     */
    public static int indexOf(final String regex, final List<String> list) {
        assert regex != null : "Regex cannot be null!";
        assert list != null : "List cannot be null!";

        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).matches(regex)) {
                return i;
            }
        }
        return -1;
    }
}

Related Tutorials