Java String Ends With endsWith(final String string, final String[] suffixes)

Here you can find the source of endsWith(final String string, final String[] suffixes)

Description

Checks a string with String#endsWith(String) against an array of candidate strings.

License

Apache License

Parameter

Parameter Description
string The string to check.
suffixes The array of suffixes to use.

Return

true if the string ends with any of the suffixes, otherwise false.

Declaration

public static boolean endsWith(final String string, final String[] suffixes) 

Method Source Code

//package com.java2s;
/*/*w  w  w .  jav  a 2s.  c  o m*/
 *  Copyright 2011 Eric F. Savage, code@efsavage.com
 *
 *   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 {
    /**
     * The Non-breaking space character.
     */
    public static final char NBSP = (char) 160;

    /**
     * Checks a string with {@link String#endsWith(String)} against an array of
     * candidate strings.
     * 
     * @param string
     *            The string to check.
     * @param suffixes
     *            The array of suffixes to use.
     * @return true if the string ends with any of the suffixes, otherwise
     *         false.
     */
    public static boolean endsWith(final String string, final String[] suffixes) {
        if (isBlank(string) || suffixes == null || suffixes.length == 0) {
            return false;
        }
        for (final String suffix : suffixes) {
            if (string.endsWith(suffix)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Looks for null or empty strings.
     * 
     * @param string
     *            String to be tested, may be null
     * @return true if string is null or zero-length
     */
    public static boolean isBlank(final String string) {
        if (string == null) {
            return true;
        }
        if (string.indexOf(NBSP) >= 0) {
            return isBlank(string.replace(NBSP, ' '));
        }
        return string.length() < 1 || string.trim().length() < 1;
    }
}

Related

  1. endsWith(final String path, final String suffix)
  2. endsWith(final String s, final String suffix, final boolean ignoreCase)
  3. endsWith(final String src, final String... suffixes)
  4. endsWith(final String str, final char suffix)
  5. endsWith(final String str, final String... suffixes)
  6. endsWith(final StringBuilder builder, final char match)
  7. endsWith(int[] data, int[] ends)
  8. endsWith(String a, char[] b)
  9. endsWith(String baseString, String compareString)