Java - Write code to get substring Before Last delimiter

Requirements

Write code to get substring Before Last delimiter

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String string = "book2s.com";
        String delimiter = "book2s.com";
        System.out.println(substringBeforeLast(string, delimiter));
    }//ww w. j  a  v a2s  . c o  m

    public static String substringBeforeLast(String string, String delimiter) {
        final int index = string.lastIndexOf(delimiter);
        if (index == -1 || string.startsWith(delimiter)) {
            return "";
        }
        return string.substring(0, index);
    }
}

Related Exercise