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

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

Description

substring After Last

License

Apache License

Declaration

public static String substringAfterLast(String str, String separator) 

Method Source Code

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

public class Main {
    public static String substringAfterLast(String str, String separator) {
        if (isEmpty(str)) {
            return str;
        }//from w  w w.j ava2  s  .  com
        if (isEmpty(separator)) {
            return "";
        }
        int pos = str.lastIndexOf(separator);
        if ((pos == -1) || (pos == str.length() - separator.length())) {
            return "";
        }
        return str.substring(pos + separator.length());
    }

    public static boolean isEmpty(String str) {
        return (str == null) || (str.length() == 0);
    }

    public static int lastIndexOf(String str, char searchChar) {
        if (isEmpty(str)) {
            return -1;
        }
        return str.lastIndexOf(searchChar);
    }

    public static int lastIndexOf(String str, char searchChar, int startPos) {
        if (isEmpty(str)) {
            return -1;
        }
        return str.lastIndexOf(searchChar, startPos);
    }

    public static int lastIndexOf(String str, String searchStr) {
        if ((str == null) || (searchStr == null)) {
            return -1;
        }
        return str.lastIndexOf(searchStr);
    }

    public static int lastIndexOf(String str, String searchStr, int startPos) {
        if ((str == null) || (searchStr == null)) {
            return -1;
        }
        return str.lastIndexOf(searchStr, startPos);
    }

    public static String substring(String str, int start) {
        if (str == null) {
            return null;
        }
        if (start < 0) {
            start = str.length() + start;
        }
        if (start < 0) {
            start = 0;
        }
        if (start > str.length()) {
            return "";
        }
        return str.substring(start);
    }

    public static String substring(String str, int start, int end) {
        if (str == null) {
            return null;
        }
        if (end < 0) {
            end = str.length() + end;
        }
        if (start < 0) {
            start = str.length() + start;
        }
        if (end > str.length()) {
            end = str.length();
        }
        if (start > end) {
            return "";
        }
        if (start < 0) {
            start = 0;
        }
        if (end < 0) {
            end = 0;
        }
        return str.substring(start, end);
    }
}

Related

  1. substringAfterLast(final String str, final String separator)
  2. substringAfterLast(final String str, final String separator)
  3. substringAfterLast(final String value, final String searchValue)
  4. substringAfterLast(String input, String delimiter)
  5. substringAfterLast(String str, String pattern)
  6. substringAfterLast(String str, String separator)
  7. substringAfterLast(String string, String delimiter)
  8. substringAfterLast(String text, char after)
  9. subStringAfterLastSymbol(String inStr, char symbol)