Java - Write code to get substring Before First delimiter

Requirements

Write code to get substring Before First delimiter

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String string = "book2s.com";
        String delimiter = "o";
        System.out.println(substringBeforeFirst(string, delimiter));
    }/*from   w ww. j a  v a 2 s  . co  m*/

    public static String substringBeforeFirst(String string,
            String delimiter) {
        final int index = string.indexOf(delimiter);
        if (index == -1) {
            return "";
        }
        return string.substring(0, index);
    }
}

Related Exercise