Java String Ends With endsWithIgnoreCase(Object string, Object suffix)

Here you can find the source of endsWithIgnoreCase(Object string, Object suffix)

Description

ends With Ignore Case

License

Open Source License

Declaration

public static final boolean endsWithIgnoreCase(Object string, Object suffix) 

Method Source Code

//package com.java2s;
/*//  w  w w  . j  a  v a2 s  . c  o  m
 * This file is part of the Jose Project
 * see http://jose-chess.sourceforge.net/
 * (c) 2002-2006 Peter Sch?fer
 *
 * 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 2 of the License, or
 * (at your option) any later version.
 *
 */

public class Main {
    public static final boolean endsWithIgnoreCase(Object string, Object suffix) {
        return endsWith(string, suffix, length(string), true);
    }

    public static final boolean endsWithIgnoreCase(String string, String suffix, int toffset) {
        return string.regionMatches(true, toffset - suffix.length(), suffix, 0, suffix.length());
    }

    public static final boolean endsWithIgnoreCase(String string, StringBuffer suffix, int toffset) {
        return regionMatchesIgnoreCase(string, toffset - suffix.length(), suffix, 0, suffix.length());
    }

    public static final boolean endsWithIgnoreCase(StringBuffer string, String suffix, int toffset) {
        return regionMatchesIgnoreCase(string, toffset - suffix.length(), suffix, 0, suffix.length());
    }

    public static final boolean endsWithIgnoreCase(StringBuffer string, StringBuffer suffix, int toffset) {
        return regionMatchesIgnoreCase(string, toffset - suffix.length(), suffix, 0, suffix.length());
    }

    public static final boolean endsWithIgnoreCase(char[] string, String suffix, int toffset) {
        return regionMatchesIgnoreCase(string, toffset - suffix.length(), suffix, 0, suffix.length());
    }

    public static final boolean endsWith(Object string, Object suffix, int toffset, boolean ignoreCase) {
        if (string instanceof String) {
            if (suffix instanceof String) {
                if (ignoreCase)
                    return endsWithIgnoreCase((String) string, (String) suffix, toffset);
                else
                    return endsWith((String) string, (String) suffix, toffset);
            }
            if (suffix instanceof StringBuffer) {
                if (ignoreCase)
                    return endsWithIgnoreCase((String) string, (StringBuffer) suffix, toffset);
                else
                    return endsWith((String) string, (StringBuffer) suffix, toffset);
            }
        }
        if (string instanceof StringBuffer) {
            if (suffix instanceof String) {
                if (ignoreCase)
                    return endsWithIgnoreCase((StringBuffer) string, (String) suffix, toffset);
                else
                    return endsWith((StringBuffer) string, (String) suffix, toffset);
            }
            if (suffix instanceof StringBuffer) {
                if (ignoreCase)
                    return endsWithIgnoreCase((StringBuffer) string, (StringBuffer) suffix, toffset);
                else
                    return endsWith((StringBuffer) string, (StringBuffer) suffix, toffset);
            }
        }
        if (string instanceof char[]) {
            if (suffix instanceof String) {
                if (ignoreCase)
                    if (ignoreCase)
                        return endsWithIgnoreCase((StringBuffer) string, (StringBuffer) suffix, toffset);
                    else
                        return endsWith((StringBuffer) string, (StringBuffer) suffix, toffset);
            } else
                throw new IllegalArgumentException("not implemented");
        }
        throw new IllegalArgumentException("expecting String or StringBuffer");
    }

    public static final boolean endsWith(Object string, Object suffix, boolean ignoreCase) {
        return endsWith(string, suffix, length(string), ignoreCase);
    }

    public static final boolean endsWith(String string, String suffix, int toffset) {
        return string.regionMatches(false, toffset - suffix.length(), suffix, 0, suffix.length());
    }

    public static final boolean endsWith(String string, StringBuffer suffix, int toffset) {
        return regionMatches(string, toffset - suffix.length(), suffix, 0, suffix.length());
    }

    public static final boolean endsWith(StringBuffer string, String suffix, int toffset) {
        return regionMatches(string, toffset - suffix.length(), suffix, 0, suffix.length());
    }

    public static final boolean endsWith(StringBuffer string, StringBuffer suffix, int toffset) {
        return regionMatches(string, toffset - suffix.length(), suffix, 0, suffix.length());
    }

    public static final boolean endsWith(char[] string, String suffix, int toffset) {
        return regionMatches(string, toffset - suffix.length(), suffix, 0, suffix.length());
    }

    public static final int length(Object string) {
        if (string instanceof String)
            return ((String) string).length();
        if (string instanceof StringBuffer)
            return ((StringBuffer) string).length();
        throw new IllegalArgumentException("expecting String or StringBuffer");
    }

    /**
     * similar to the String.regionMatches but works on both String and StringBuffer
     */
    public static final boolean regionMatches(Object string, boolean ignoreCase, int toffset, Object other,
            int ooffset, int len) {
        if (string instanceof String) {
            if (other instanceof String)
                return ((String) string).regionMatches(ignoreCase, toffset, (String) other, ooffset, len);
            if (other instanceof StringBuffer) {
                if (ignoreCase)
                    return regionMatchesIgnoreCase((String) string, toffset, (StringBuffer) other, ooffset, len);
                else
                    return regionMatches((String) string, toffset, (StringBuffer) other, ooffset, len);
            }
        }
        if (string instanceof StringBuffer) {
            if (other instanceof String) {
                if (ignoreCase)
                    return regionMatchesIgnoreCase((StringBuffer) string, toffset, (String) other, ooffset, len);
                else
                    return regionMatches((StringBuffer) string, toffset, (String) other, ooffset, len);
            }
            if (other instanceof StringBuffer) {
                if (ignoreCase)
                    return regionMatchesIgnoreCase((StringBuffer) string, toffset, (StringBuffer) other, ooffset,
                            len);
                else
                    return regionMatches((StringBuffer) string, toffset, (StringBuffer) other, ooffset, len);
            }
        }
        if (string instanceof char[]) {
            if (other instanceof String) {
                if (ignoreCase)
                    return regionMatchesIgnoreCase((char[]) string, toffset, (String) other, ooffset, len);
                else
                    return regionMatches((char[]) string, toffset, (String) other, ooffset, len);
            } else
                throw new IllegalArgumentException("expecting String or StringBuffer");
        }
        throw new IllegalArgumentException("expecting String or StringBuffer");
    }

    public static final boolean regionMatches(char[] string, int toffset, String other, int ooffset, int len) {
        if (toffset < 0 || (toffset + len) > string.length)
            return false;
        if (ooffset < 0 || (ooffset + len) > other.length())
            return false;

        while (len-- > 0) {
            if (string[toffset++] != other.charAt(ooffset++))
                return false;
        }

        return true;
    }

    public static final boolean regionMatches(String string, int toffset, StringBuffer other, int ooffset,
            int len) {
        if (toffset < 0 || (toffset + len) > string.length())
            return false;
        if (ooffset < 0 || (ooffset + len) > other.length())
            return false;

        while (len-- > 0) {
            if (string.charAt(toffset++) != other.charAt(ooffset++))
                return false;
        }

        return true;
    }

    public static final boolean regionMatches(StringBuffer string, int toffset, String other, int ooffset,
            int len) {
        if (toffset < 0 || (toffset + len) > string.length())
            return false;
        if (ooffset < 0 || (ooffset + len) > other.length())
            return false;

        while (len-- > 0) {
            if (string.charAt(toffset++) != other.charAt(ooffset++))
                return false;
        }

        return true;
    }

    public static final boolean regionMatches(StringBuffer string, int toffset, StringBuffer other, int ooffset,
            int len) {
        if (toffset < 0 || (toffset + len) > string.length())
            return false;
        if (ooffset < 0 || (ooffset + len) > other.length())
            return false;

        while (len-- > 0) {
            if (string.charAt(toffset++) != other.charAt(ooffset++))
                return false;
        }

        return true;
    }

    public static final boolean regionMatches(String string, int toffset, char[] other, int ooffset, int len) {
        if (toffset < 0 || (toffset + len) > string.length())
            return false;
        if (ooffset < 0 || (ooffset + len) > other.length)
            return false;

        while (len-- > 0) {
            if (string.charAt(toffset++) != other[ooffset++])
                return false;
        }

        return true;
    }

    private static boolean regionMatchesIgnoreCase(char[] string, int toffset, String other, int ooffset, int len) {
        if (toffset < 0 || (toffset + len) > string.length)
            return false;
        if (ooffset < 0 || (ooffset + len) > other.length())
            return false;

        while (len-- > 0) {
            if (Character.toUpperCase(string[toffset++]) != Character.toUpperCase(other.charAt(ooffset++)))
                return false;
        }

        return true;
    }

    public static final boolean regionMatchesIgnoreCase(String string, int toffset, StringBuffer other, int ooffset,
            int len) {
        if (toffset < 0 || (toffset + len) > string.length())
            return false;
        if (ooffset < 0 || (ooffset + len) > other.length())
            return false;

        while (len-- > 0) {
            if (Character.toUpperCase(string.charAt(toffset++)) != Character.toUpperCase(other.charAt(ooffset++)))
                return false;
        }

        return true;
    }

    public static final boolean regionMatchesIgnoreCase(StringBuffer string, int toffset, String other, int ooffset,
            int len) {
        if (toffset < 0 || (toffset + len) > string.length())
            return false;
        if (ooffset < 0 || (ooffset + len) > other.length())
            return false;

        while (len-- > 0) {
            if (Character.toUpperCase(string.charAt(toffset++)) != Character.toUpperCase(other.charAt(ooffset++)))
                return false;
        }

        return true;
    }

    public static final boolean regionMatchesIgnoreCase(StringBuffer string, int toffset, StringBuffer other,
            int ooffset, int len) {
        if (toffset < 0 || (toffset + len) > string.length())
            return false;
        if (ooffset < 0 || (ooffset + len) > other.length())
            return false;

        while (len-- > 0) {
            if (Character.toUpperCase(string.charAt(toffset++)) != Character.toUpperCase(other.charAt(ooffset++)))
                return false;
        }

        return true;
    }

    public static final boolean regionMatchesIgnoreCase(String string, int toffset, char[] other, int ooffset,
            int len) {
        if (toffset < 0 || (toffset + len) > string.length())
            return false;
        if (ooffset < 0 || (ooffset + len) > other.length)
            return false;

        while (len-- > 0) {
            if (Character.toUpperCase(string.charAt(toffset++)) != Character.toUpperCase(other[ooffset++]))
                return false;
        }

        return true;
    }
}

Related

  1. endsWithIgnoreCase(final String str, final String end)
  2. endsWithIgnoreCase(final String string, final String end)
  3. endsWithIgnoreCase(final String text, final String suffix)
  4. endsWithIgnoreCase(final String text, final String suffix)
  5. endsWithIgnoreCase(final String text, final String suffix)
  6. endsWithIgnoreCase(String a, String b)
  7. endsWithIgnoreCase(String baseString, String compareString)
  8. endsWithIgnoreCase(String haystack, String needle)
  9. endsWithIgnoreCase(String input, String suffix)