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

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

Description

ends With

License

MIT License

Declaration

public static boolean endsWith(CharSequence str, char suffix) 

Method Source Code

//package com.java2s;
/*!// ww w .j av  a  2s. c  o m
 * mifmi-commons4j
 * https://github.com/mifmi/mifmi-commons4j
 *
 * Copyright (c) 2015 mifmi.org and other contributors
 * Released under the MIT license
 * https://opensource.org/licenses/MIT
 */

public class Main {
    public static boolean endsWith(CharSequence str, char suffix) {
        if (str == null) {
            return false;
        }

        if (str.length() < 1) {
            return false;
        }

        char lastCh = str.charAt(str.length() - 1);
        return (lastCh == suffix);
    }

    public static boolean endsWith(CharSequence str, CharSequence suffix) {
        if (str == null) {
            return false;
        }

        if (suffix == null || suffix.length() == 0) {
            return true;
        }

        int len = suffix.length();
        int offset = str.length() - len;

        if (offset < 0) {
            return false;
        }

        for (int i = 0; i < len; i++) {
            char ch = str.charAt(i + offset);
            char suffixCh = suffix.charAt(i);

            if (ch != suffixCh) {
                return false;
            }
        }

        return true;
    }

    public static CharSequence endsWith(CharSequence str, CharSequence... suffixes) {
        if (str == null) {
            return null;
        }

        if (suffixes == null || suffixes.length == 0) {
            return null;
        }

        for (CharSequence suffix : suffixes) {
            if (endsWith(str, suffix)) {
                return suffix;
            }
        }

        return null;
    }
}

Related

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