Java String Ends With endsWith(final boolean caseSensitive, final String text, final String suffix)

Here you can find the source of endsWith(final boolean caseSensitive, final String text, final String suffix)

Description

Checks whether a text ends with a specified suffix.

License

Apache License

Parameter

Parameter Description
caseSensitive whether the comparison must be done in a case-sensitive or case-insensitive way.
text the text to be checked for suffixes.
suffix the suffix to be searched.

Return

whether the text ends with the suffix or not.

Declaration

public static boolean endsWith(final boolean caseSensitive, final String text, final String suffix) 

Method Source Code

//package com.java2s;
/*//www  . j a v a 2 s. c o  m
 * =============================================================================
 * 
 *   Copyright (c) 2012-2014, The ATTOPARSER team (http://www.attoparser.org)
 * 
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * 
 * =============================================================================
 */

public class Main {
    /**
     * <p>
     *   Checks whether a text ends with a specified suffix.
     * </p>
     *
     * @param caseSensitive whether the comparison must be done in a case-sensitive or case-insensitive way.
     * @param text the text to be checked for suffixes.
     * @param suffix the suffix to be searched.
     * @return whether the text ends with the suffix or not.
     */
    public static boolean endsWith(final boolean caseSensitive, final String text, final String suffix) {

        if (text == null) {
            throw new IllegalArgumentException("Text cannot be null");
        }
        if (suffix == null) {
            throw new IllegalArgumentException("Suffix cannot be null");
        }

        return (caseSensitive ? text.endsWith(suffix)
                : endsWith(caseSensitive, text, 0, text.length(), suffix, 0, suffix.length()));

    }

    /**
     * <p>
     *   Checks whether a text ends with a specified suffix.
     * </p>
     *
     * @param caseSensitive whether the comparison must be done in a case-sensitive or case-insensitive way.
     * @param text the text to be checked for suffixes.
     * @param suffix the suffix to be searched.
     * @return whether the text ends with the suffix or not.
     */
    public static boolean endsWith(final boolean caseSensitive, final String text, final char[] suffix) {
        return endsWith(caseSensitive, text, 0, text.length(), suffix, 0, suffix.length);
    }

    /**
     * <p>
     *   Checks whether a text ends with a specified suffix.
     * </p>
     *
     * @param caseSensitive whether the comparison must be done in a case-sensitive or case-insensitive way.
     * @param text the text to be checked for suffixes.
     * @param suffix the suffix to be searched.
     * @return whether the text ends with the suffix or not.
     */
    public static boolean endsWith(final boolean caseSensitive, final char[] text, final char[] suffix) {
        return endsWith(caseSensitive, text, 0, text.length, suffix, 0, suffix.length);
    }

    /**
     * <p>
     *   Checks whether a text ends with a specified suffix, specifying (offset,len) pairs
     *   for limiting the fragments to be checked.
     * </p>
     *
     * @param caseSensitive whether the comparison must be done in a case-sensitive or case-insensitive way.
     * @param text the text to be checked for suffixes.
     * @param textOffset the offset of the text.
     * @param textLen the length of the text.
     * @param suffix the suffix to be searched.
     * @param suffixOffset the offset of the suffix.
     * @param suffixLen the length of the suffix.
     * @return whether the text ends with the suffix or not.
     */
    public static boolean endsWith(final boolean caseSensitive, final char[] text, final int textOffset,
            final int textLen, final char[] suffix, final int suffixOffset, final int suffixLen) {

        if (text == null) {
            throw new IllegalArgumentException("Text cannot be null");
        }
        if (suffix == null) {
            throw new IllegalArgumentException("Suffix cannot be null");
        }

        if (textLen < suffixLen) {
            return false;
        }

        final int textReverseOffset = textOffset + textLen - 1;
        final int suffixReverseOffset = suffixOffset + suffixLen - 1;

        char c1, c2;

        int n = suffixLen;
        int i = 0;

        while (n-- != 0) {

            c1 = text[textReverseOffset - i];
            c2 = suffix[suffixReverseOffset - i];

            if (c1 != c2) {

                if (caseSensitive) {
                    return false;
                }

                c1 = Character.toUpperCase(c1);
                c2 = Character.toUpperCase(c2);

                if (c1 != c2) {

                    // We check both upper and lower case because that is how String#equalsIgnoreCase() is defined.
                    // See String#regionMatches(boolean,int,String,int,int)
                    if (Character.toLowerCase(c1) != Character.toLowerCase(c2)) {
                        return false;
                    }

                }

            }

            i++;

        }

        return true;

    }

    /**
     * <p>
     *   Checks whether a text ends with a specified suffix, specifying (offset,len) pairs
     *   for limiting the fragments to be checked.
     * </p>
     *
     * @param caseSensitive whether the comparison must be done in a case-sensitive or case-insensitive way.
     * @param text the text to be checked for suffixes.
     * @param textOffset the offset of the text.
     * @param textLen the length of the text.
     * @param suffix the suffix to be searched.
     * @param suffixOffset the offset of the suffix.
     * @param suffixLen the length of the suffix.
     * @return whether the text ends with the suffix or not.
     */
    public static boolean endsWith(final boolean caseSensitive, final String text, final int textOffset,
            final int textLen, final char[] suffix, final int suffixOffset, final int suffixLen) {

        if (text == null) {
            throw new IllegalArgumentException("Text cannot be null");
        }
        if (suffix == null) {
            throw new IllegalArgumentException("Suffix cannot be null");
        }

        if (textLen < suffixLen) {
            return false;
        }

        final int textReverseOffset = textOffset + textLen - 1;
        final int suffixReverseOffset = suffixOffset + suffixLen - 1;

        char c1, c2;

        int n = suffixLen;
        int i = 0;

        while (n-- != 0) {

            c1 = text.charAt(textReverseOffset - i);
            c2 = suffix[suffixReverseOffset - i];

            if (c1 != c2) {

                if (caseSensitive) {
                    return false;
                }

                c1 = Character.toUpperCase(c1);
                c2 = Character.toUpperCase(c2);

                if (c1 != c2) {

                    // We check both upper and lower case because that is how String#equalsIgnoreCase() is defined.
                    // See String#regionMatches(boolean,int,String,int,int)
                    if (Character.toLowerCase(c1) != Character.toLowerCase(c2)) {
                        return false;
                    }

                }

            }

            i++;

        }

        return true;

    }

    /**
     * <p>
     *   Checks whether a text ends with a specified suffix, specifying (offset,len) pairs
     *   for limiting the fragments to be checked.
     * </p>
     *
     * @param caseSensitive whether the comparison must be done in a case-sensitive or case-insensitive way.
     * @param text the text to be checked for suffixes.
     * @param textOffset the offset of the text.
     * @param textLen the length of the text.
     * @param suffix the suffix to be searched.
     * @param suffixOffset the offset of the suffix.
     * @param suffixLen the length of the suffix.
     * @return whether the text ends with the suffix or not.
     */
    public static boolean endsWith(final boolean caseSensitive, final char[] text, final int textOffset,
            final int textLen, final String suffix, final int suffixOffset, final int suffixLen) {

        if (text == null) {
            throw new IllegalArgumentException("Text cannot be null");
        }
        if (suffix == null) {
            throw new IllegalArgumentException("Suffix cannot be null");
        }

        if (textLen < suffixLen) {
            return false;
        }

        final int textReverseOffset = textOffset + textLen - 1;
        final int suffixReverseOffset = suffixOffset + suffixLen - 1;

        char c1, c2;

        int n = suffixLen;
        int i = 0;

        while (n-- != 0) {

            c1 = text[textReverseOffset - i];
            c2 = suffix.charAt(suffixReverseOffset - i);

            if (c1 != c2) {

                if (caseSensitive) {
                    return false;
                }

                c1 = Character.toUpperCase(c1);
                c2 = Character.toUpperCase(c2);

                if (c1 != c2) {

                    // We check both upper and lower case because that is how String#equalsIgnoreCase() is defined.
                    // See String#regionMatches(boolean,int,String,int,int)
                    if (Character.toLowerCase(c1) != Character.toLowerCase(c2)) {
                        return false;
                    }

                }

            }

            i++;

        }

        return true;

    }

    /**
     * <p>
     *   Checks whether a text ends with a specified suffix, specifying (offset,len) pairs
     *   for limiting the fragments to be checked.
     * </p>
     *
     * @param caseSensitive whether the comparison must be done in a case-sensitive or case-insensitive way.
     * @param text the text to be checked for suffixes.
     * @param textOffset the offset of the text.
     * @param textLen the length of the text.
     * @param suffix the suffix to be searched.
     * @param suffixOffset the offset of the suffix.
     * @param suffixLen the length of the suffix.
     * @return whether the text ends with the suffix or not.
     */
    public static boolean endsWith(final boolean caseSensitive, final String text, final int textOffset,
            final int textLen, final String suffix, final int suffixOffset, final int suffixLen) {

        if (text == null) {
            throw new IllegalArgumentException("Text cannot be null");
        }
        if (suffix == null) {
            throw new IllegalArgumentException("Suffix cannot be null");
        }

        if (textLen < suffixLen) {
            return false;
        }

        final int textReverseOffset = textOffset + textLen - 1;
        final int suffixReverseOffset = suffixOffset + suffixLen - 1;

        char c1, c2;

        int n = suffixLen;
        int i = 0;

        while (n-- != 0) {

            c1 = text.charAt(textReverseOffset - i);
            c2 = suffix.charAt(suffixReverseOffset - i);

            if (c1 != c2) {

                if (caseSensitive) {
                    return false;
                }

                c1 = Character.toUpperCase(c1);
                c2 = Character.toUpperCase(c2);

                if (c1 != c2) {

                    // We check both upper and lower case because that is how String#equalsIgnoreCase() is defined.
                    // See String#regionMatches(boolean,int,String,int,int)
                    if (Character.toLowerCase(c1) != Character.toLowerCase(c2)) {
                        return false;
                    }

                }

            }

            i++;

        }

        return true;

    }
}

Related

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