Java String Suffix suffixWith(String receiver, char suffix, boolean ignoreCase)

Here you can find the source of suffixWith(String receiver, char suffix, boolean ignoreCase)

Description

suffix With

License

Open Source License

Declaration

public static String suffixWith(String receiver, char suffix, boolean ignoreCase) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static String suffixWith(String receiver, char suffix) {
        return suffixWith(receiver, suffix, false);
    }/*from   ww w  .j a  va 2s.c o m*/

    public static String suffixWith(String receiver, char suffix, boolean ignoreCase) {
        if (receiver != null && !receiver.isEmpty() && !endsWith(receiver, String.valueOf(suffix), ignoreCase)) {
            return receiver + suffix;
        }
        return orEmpty(receiver);
    }

    public static String suffixWith(String receiver, String suffix) {
        return suffixWith(receiver, suffix, false);
    }

    public static String suffixWith(String receiver, String suffix, boolean ignoreCase) {
        if (receiver != null && !receiver.isEmpty() && !suffix.isEmpty()
                && !endsWith(receiver, suffix, ignoreCase)) {
            return receiver + (suffix);
        }
        return orEmpty(receiver);
    }

    public static boolean endsWith(String receiver, String... needles) {
        return endsWith(receiver, false, needles);
    }

    public static boolean endsWith(String receiver, boolean ignoreCase, String... needles) {
        if (receiver == null)
            return false;

        for (String needle : needles) {
            if (endsWith(receiver, ignoreCase, needle)) {
                return true;
            }
        }
        return false;
    }

    public static boolean endsWith(CharSequence receiver, String suffix, boolean ignoreCase) {
        return receiver.length() >= suffix.length() && regionMatches(receiver, receiver.length() - suffix.length(),
                suffix, 0, suffix.length(), ignoreCase);
    }

    public static String orEmpty(String receiver) {
        return receiver == null ? "" : receiver;
    }

    public static boolean regionMatches(CharSequence receiver, int thisOffset, String other, int otherOffset,
            int length, boolean ignoreCase) {
        if (ignoreCase) {
            for (int i = 0; i < length; i++) {
                if (Character.toLowerCase(receiver.charAt(i + thisOffset)) != Character
                        .toLowerCase(other.charAt(i + otherOffset)))
                    return false;
            }
        } else {
            for (int i = 0; i < length; i++) {
                if (receiver.charAt(i + thisOffset) != other.charAt(i + otherOffset))
                    return false;
            }
        }
        return true;
    }
}

Related

  1. suffixClassName(String tableName)
  2. suffixes(char[] x, int[] suff)
  3. suffixLabel(String label, String tagSuffix)
  4. suffixName(String name)
  5. suffixNotNullStringOrDefault(String str, String defaultStr, String suffix)