Java String Match Count countMatches(String[] query, String[] entry)

Here you can find the source of countMatches(String[] query, String[] entry)

Description

Counts how many Strings in the entry array start with the Strings of the query array.

License

Open Source License

Declaration

private static int countMatches(String[] query, String[] entry) 

Method Source Code

//package com.java2s;
/*/*from w  w  w  .  j  a v  a  2 s  .c o  m*/
 * Copyright (C) 2014 Bernardo Sulzbach
 *
 * 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.
 *
 * 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/>.
 */

public class Main {
    /**
     * Counts how many Strings in the entry array start with the Strings of the query array.
     */
    private static int countMatches(String[] query, String[] entry) {
        int matches = 0;
        int indexOfLastMatchPlusOne = 0;
        for (int i = 0; i < query.length && indexOfLastMatchPlusOne < entry.length; i++) {
            for (int j = indexOfLastMatchPlusOne; j < entry.length; j++) {
                if (startsWithIgnoreCase(entry[j], query[i])) {
                    indexOfLastMatchPlusOne = j + 1;
                    matches++;
                }
            }
        }
        return matches;
    }

    /**
     * Checks if a string starts with a given string, ignoring case differences.
     *
     * @param a the base string.
     * @param b the prefix.
     * @return true, if the base string starts with the prefix, ignoring case differences.
     */
    public static boolean startsWithIgnoreCase(String a, String b) {
        return a.toLowerCase().startsWith(b.toLowerCase());
    }
}

Related

  1. countMatches(String str, String sub)
  2. countMatches(String string, char find)
  3. countMatches(String string, char toMatch)
  4. countMatches(String string, String other)
  5. countMatches(String theString, String... sep)
  6. countMatchingLines(Throwable ex, Throwable cause)