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));
    }/*  w  w  w .  j  ava 2  s.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