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

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

Description

Tests if a char sequence ends with some suffix

License

Open Source License

Parameter

Parameter Description
cs the first char sequence to test
suffix the suffix to test

Return

true if cs ends with suffix

Declaration

public static boolean endsWith(CharSequence cs, CharSequence suffix) 

Method Source Code

//package com.java2s;

public class Main {
    /** Tests if a char sequence ends with some suffix
     * @param   cs      the first char sequence to test
     * @param   suffix   the suffix to test
     * @return true if cs ends with suffix
     *///from   w w w.  j av a  2 s  .c  om
    public static boolean endsWith(CharSequence cs, CharSequence suffix) {
        return startsWith(cs, suffix, (cs.length() - suffix.length()));
    }

    /** Tests if a char sequence ends with some suffix
     * @param   cs            the first char sequence to test
     * @param   suffix         the suffix to test
     * @param   caseSensitive   do case sensitive comparison?
     * @return true if cs ends with suffix
     */
    public static boolean endsWith(CharSequence cs, CharSequence suffix, boolean caseSensitive) {
        return startsWith(cs, suffix, (cs.length() - suffix.length()), caseSensitive);
    }

    /** Tests if a char sequence starts with some prefix
     * @param   cs      the char sequence to test
     * @param   prefix   the prefix to test
     * @return true if cs starts with prefix
     */
    public static boolean startsWith(CharSequence cs, CharSequence prefix) {
        return startsWith(cs, prefix, 0, true);
    }

    /** Tests if a char sequence starts with some prefix
     * @param   cs            the char sequence to test
     * @param   prefix         the prefix to test
     * @param   caseSensitive   do case sensitive comparison?
     * @return true if cs starts with prefix
     */
    public static boolean startsWith(CharSequence cs, CharSequence prefix, boolean caseSensitive) {
        return startsWith(cs, prefix, 0, caseSensitive);
    }

    /** Tests if a char sequence starts with some prefix at a specific offset
     * @param   cs      the char sequence to test
     * @param   prefix   the prefix to test
     * @param   offset   the offset to look at
     * @return true if prefix lies in cs, starting at the specified offset 
     */
    public static boolean startsWith(CharSequence cs, CharSequence prefix, int offset) {
        return startsWith(cs, prefix, offset, true);
    }

    /** Tests if a char sequence starts with some prefix at a specific offset
     * @param   cs            the char sequence to test
     * @param   prefix         the prefix to test
     * @param   offset         the offset to look at
     * @param   caseSensitive   do case sensitive comparison?
     * @return true if prefix lies in cs, starting at the specified offset 
     */
    public static boolean startsWith(CharSequence cs, CharSequence prefix, int offset, boolean caseSensitive) {
        if (offset < 0)
            return false;
        int prefixLength = prefix.length();
        if (cs.length() < (offset + prefixLength))
            return false;
        int c;
        for (c = 0; (c < prefixLength) && ((c + offset) < cs.length()); c++)
            if (caseSensitive && cs.charAt(offset + c) != prefix.charAt(c))
                return false;
            else if (!caseSensitive
                    && Character.toLowerCase(cs.charAt(offset + c)) != Character.toLowerCase(prefix.charAt(c)))
                return false;
        return (c == prefixLength);
    }
}

Related

  1. endsWith(byte[] dataFrom, String suffix)
  2. endsWith(byte[] source, char c)
  3. endsWith(byte[] subject, byte[] suffix)
  4. endsWith(char s[], int len, char suffix[])
  5. endsWith(char[] fieldDescriptor, char c)
  6. endsWith(CharSequence cs, String postfix)
  7. endsWith(CharSequence s, char c)
  8. endsWith(CharSequence s, char c)
  9. endsWith(CharSequence s, CharSequence seq)