Java - Get substring Before Last separator

Description

Get substring Before Last separator

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String str = "book2s.com";
        String separator = "book2s.com";
        System.out.println(substringBeforeLast(str, separator));
    }/*from  w  w  w  . j a v a 2s  . c  o m*/

    public static final String EMPTY_STRING = "";

    public static String substringBeforeLast(String str, String separator) {
        if ((str == null) || (separator == null) || (str.length() == 0)
                || (separator.length() == 0)) {
            return str;
        }

        int pos = str.lastIndexOf(separator);

        if (pos == -1) {
            return str;
        }

        return str.substring(0, pos);
    }

}

Get substring Before Last separator

Related Exercise