Java String Starts Wtih startsWithLenient(final String s, final String[] matches, final int[] minChars, final boolean acceptTrailing)

Here you can find the source of startsWithLenient(final String s, final String[] matches, final int[] minChars, final boolean acceptTrailing)

Description

Returns if a string shares at least a specified numbers starting characters with a number of matches.

License

Open Source License

Parameter

Parameter Description
s The string to check. Not <code>null</code>.
matches A number of possible starts for <code>s</code>.
minChars The minimum number of characters to match for every element in <code>matches</code>. Needs to be of same length as <code>matches</code>. Can be <code>null</code>.
acceptTrailing If after the required number of characters are matched on recognized characters that are not in one of the the <code>matches</code> string should be accepted. For instance if "abczz" should be matched with "abcdef" and min chars 3.

Return

The index of the first unmatched character if minChars was reached or -1 if a match was not found.

Declaration

private static int startsWithLenient(final String s, final String[] matches, final int[] minChars,
        final boolean acceptTrailing) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from www.  jav  a  2s .c o  m*/
     * Returns if a string shares at least a specified numbers starting characters with a number of matches.
     * <p>
     * This method just exercises {@link #startsWithLenient(String, String, int, boolean)} with every one of <code>matches</code>
     * and <code>minChars</code>.
     * 
     * @param s The string to check. Not <code>null</code>.
     * @param matches A number of possible starts for <code>s</code>.
     * @param minChars The minimum number of characters to match for every element in <code>matches</code>. Needs
     *            to be of same length as <code>matches</code>. Can be <code>null</code>.
     * @param acceptTrailing If after the required number of characters are matched on recognized characters that are not
     *            in one of the the <code>matches</code> string should be accepted. For instance if "abczz" should be matched with
     *            "abcdef" and min chars 3.
     * @return The index of the first unmatched character if <code>minChars</code> was reached or <code>-1</code> if a match was
     *         not
     *         found.
     */
    private static int startsWithLenient(final String s, final String[] matches, final int[] minChars,
            final boolean acceptTrailing) {
        for (int i = 0; i < matches.length; i++) {
            final int minChar = minChars != null ? minChars[i] : -1;
            final int ix = startsWithLenient(s, matches[i], minChar, acceptTrailing);
            if (ix > -1) {
                return ix;
            }
        }
        return -1;
    }

    /**
     * Returns if a string shares at least a specified numbers starting characters with a match.
     * 
     * @param s The string to check. Not <code>null</code> and must be trimmed.
     * @param match The possible start for <code>s</code>. Not <code>null</code> and must be trimmed.
     * @param minChars The mimimum number of characters to match to <code>s</code> for it this to be considered a match. -1 means
     *            the full length of <code>match</code>.
     * @param acceptTrailing If after the required number of charecters are matched unrecognized characters that are not
     *            in one of the the <code>matches</code> string should be accepted. For instance if "abczz" should be matched with
     *            "abcdef" and min chars 3.
     * @return The index of the first unmatched character if <code>minChars</code> was reached or <code>-1</code> if a match was
     *         not
     *         found.
     */
    private static int startsWithLenient(final String s, final String match, int minChars,
            final boolean acceptTrailing) {
        if (s.charAt(0) != match.charAt(0)) {
            return -1;
        }

        if (minChars == -1) {
            minChars = match.length();
        }

        final int sSz = s.length();
        if (sSz < minChars) {
            return -1;
        }

        final int mSz = match.length();
        int sIx = 0;
        for (int mIx = 0; mIx < mSz; sIx++, mIx++) {
            while (sIx < sSz && (s.charAt(sIx) == ' ' || s.charAt(sIx) == '_')) {
                // Disregard spaces and _
                sIx++;
            }

            if (sIx >= sSz || s.charAt(sIx) != match.charAt(mIx)) {
                return mIx >= minChars && (acceptTrailing || sIx >= sSz) && (sIx >= sSz || s.charAt(sIx - 1) == ' ')
                        ? sIx
                        : -1;
            }
        }
        return sIx >= sSz || acceptTrailing || s.charAt(sIx) == ' ' ? sIx : -1;
    }
}

Related

  1. startsWithIgnoreCaseAndWs(String searchIn, String searchFor)
  2. startsWithIgnoreCaseAndWs(String searchIn, String searchFor, int beginPos)
  3. startsWithIgnoreWhitespace(String str, String prefix)
  4. startsWithIgnoreWhitespaces(String prefix, String string)
  5. startsWithIndex(String[] prefixes, String fullPath)
  6. startsWithLetter(String str)
  7. startsWithLetterOrUnderscore(String value)
  8. startsWithLinuxRoot(String path)
  9. startsWithLowerCase(String text)