Java String Ends With endsWith(String a, char[] b)

Here you can find the source of endsWith(String a, char[] b)

Description

Equivalent of String.endsWith(String) using a char[].

License

Open Source License

Parameter

Parameter Description
a String to test.
b suffix to test.

Return

true if a ends with b.

Declaration

public static boolean endsWith(String a, char[] b) 

Method Source Code

//package com.java2s;
/*//from w ww  . ja  v  a 2 s  . co  m
 * This file is part of muCommander, http://www.mucommander.com
 * Copyright (C) 2002-2010 Maxence Bernard
 *
 * muCommander 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.
 *
 * muCommander 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 {
    /**
     * Equivalent of <code>String.endsWith(String)</code> using a <code>char[]</code>.
     * @param  a String to test.
     * @param  b suffix to test.
     * @return   <code>true</code> if <code>a</code> ends with <code>b</code>.
     */
    public static boolean endsWith(String a, char[] b) {
        return matches(a, b, a.length());
    }

    /**
     * Returns <code>true</code> if the substring of <code>a</code> starting at <code>posA</code> matches <code>b</code>.
     * @param  a    String to test.
     * @param  b    substring to look for.
     * @param  posA position in <code>a</code> at which to look for <code>b</code>
     * @return      <code>true</code> if <code>a</code> contains <code>b</code> at position <code>posA</code>, <code>false</code> otherwise..
     */
    public static boolean matches(String a, char[] b, int posA) {
        int posB;

        if (posA < (posB = b.length))
            return false;
        while (posB > 0)
            if (a.charAt(--posA) != b[--posB])
                return false;
        return true;
    }
}

Related

  1. endsWith(final String str, final char suffix)
  2. endsWith(final String str, final String... suffixes)
  3. endsWith(final String string, final String[] suffixes)
  4. endsWith(final StringBuilder builder, final char match)
  5. endsWith(int[] data, int[] ends)
  6. endsWith(String baseString, String compareString)
  7. endsWith(String filePath, String extension)
  8. endsWith(String fullString, String subString)
  9. endsWith(String haystack, String needle)