Java String Sub String substringBeforeLastChar(String str, String separator)

Here you can find the source of substringBeforeLastChar(String str, String separator)

Description

substring Before Last Char

License

Apache License

Declaration

public static String substringBeforeLastChar(String str, String separator) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    private static final int INDEX_NOT_FOUND = -1;

    public static String substringBeforeLastChar(String str, String separator) {
        if (isEmpty(str) || isEmpty(separator)) {
            return str;
        }/*from  w w w.  jav  a2  s  .co  m*/
        int pos = str.lastIndexOf(separator);
        if (pos == INDEX_NOT_FOUND) {
            return str;
        }
        if (pos == 0) {
            return "";
        }
        return str.substring(pos - 1, pos);
    }

    /**
     * <p>
     * Checks if a CharSequence is empty ("") or null.
     * </p>
     * <p>
     * <pre>
     * StringUtils.isEmpty(null)      = true
     * StringUtils.isEmpty("")        = true
     * StringUtils.isEmpty(" ")       = false
     * StringUtils.isEmpty("bob")     = false
     * StringUtils.isEmpty("  bob  ") = false
     * </pre>
     * <p>
     * <p>
     * NOTE: This method changed in Lang version 2.0. It no longer trims the
     * CharSequence. That functionality is available in isBlank().
     * </p>
     *
     * @param cs the CharSequence to check, may be null
     * @return {@code true} if the CharSequence is empty or null
     * @since 3.0 Changed signature from isEmpty(String) to
     * isEmpty(CharSequence)
     */
    public static boolean isEmpty(CharSequence cs) {
        return cs == null || cs.length() == 0;
    }
}

Related

  1. substringBeforeLast(final String str, final String separator)
  2. substringBeforeLast(String str, String separator)
  3. substringBeforeLast(String str, String separator)
  4. subStringBeforeLast(String str, String separator)
  5. substringBeforeLast(String text, String separator)
  6. substringBeforeLastIgnoreCase(final String target, final String separator)
  7. substringBetween(final String s, final String tag)
  8. substringBetween(final String str, final String startToken, final String endToken)
  9. substringBetween(String line, final String delimiterBefore, final String delimiterAfter)