Java String Sub String substringBeforeLast(String text, String separator)

Here you can find the source of substringBeforeLast(String text, String separator)

Description

substring Before Last

License

Open Source License

Declaration

public static String substringBeforeLast(String text, String separator) 

Method Source Code

//package com.java2s;

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

    public static String substringBeforeLast(String text, String separator) {
        int index = lastIndexOf(text, separator);
        return substringBefore(text, index);
    }/*  w  w w.  j  a va  2s .co  m*/

    private static int lastIndexOf(String text, String separator) {
        if (isBlank(text)) {
            return INDEX_NOT_FOUND;
        }
        return text.lastIndexOf(separator);
    }

    public static String substringBefore(String text, String separator) {
        int index = indexOf(text, separator);
        return substringBefore(text, index);
    }

    private static String substringBefore(String text, int index) {
        if (index == INDEX_NOT_FOUND) {
            return text;
        }
        return text.substring(0, index);
    }

    public static boolean isBlank(String text) {
        return text == null || text.trim().isEmpty();
    }

    private static int indexOf(String text, String separator) {
        if (isBlank(text)) {
            return INDEX_NOT_FOUND;
        }
        return text.indexOf(separator);
    }
}

Related

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