Java String Ends With endsWith(CharSequence str, CharSequence suffix)

Here you can find the source of endsWith(CharSequence str, CharSequence suffix)

Description

Return true if one CharSequence ends with the other.

License

Open Source License

Declaration

public static boolean endsWith(CharSequence str, CharSequence suffix) 

Method Source Code

//package com.java2s;
/*/*w  w  w. j a  va  2 s  .  c  o  m*/
StringUtils.java : A stand alone utility class.
Copyright (C) 2000 Justin P. McCarthy
    
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
    
This library 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
Lesser General Public License for more details.
    
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see <http://www.gnu.org/licenses/>.
    
To further contact the author please email jpmccar@gjt.org
    
    
Modifications, copyright 2001-2014 Tuma Solutions, LLC; distributed under the
LGPL, as described above.
    
*/

public class Main {
    /** Return true if one CharSequence ends with the other.
     * 
     * This performs the equivalent of String.endsWith, but accepts arbitrary
     * CharSequence objects.  (Thus, it works for StringBuffer arguments.)
     */
    public static boolean endsWith(CharSequence str, CharSequence suffix) {
        // guard against null arguments
        if (str == null || suffix == null)
            return false;

        // quick optimization for pure string args
        if (str instanceof String && suffix instanceof String)
            return ((String) str).endsWith((String) suffix);

        // quick test to see if lengths make an "endsWith" impossible
        if (str.length() < suffix.length())
            return false;

        // walk over the characters in question, looking for mismatches.
        int j = str.length();
        for (int i = suffix.length(); i-- > 0;)
            if (str.charAt(--j) != suffix.charAt(i))
                return false;

        // must be a match.
        return true;
    }
}

Related

  1. endsWith(CharSequence s, CharSequence seq)
  2. endsWith(CharSequence s, CharSequence sub)
  3. endsWith(CharSequence seq, char... any)
  4. endsWith(CharSequence source, CharSequence search)
  5. endsWith(CharSequence str, char suffix)
  6. endsWith(final boolean caseSensitive, final String text, final String suffix)
  7. endsWith(final byte[] big, final byte[] suffix)
  8. endsWith(final byte[] str1, int startIndex1, int endIndex1, final byte[] str2, int startIndex2, int endIndex2)
  9. endsWith(final CharSequence a, final CharSequence b)