Java - Write code to get substring Before separator

Requirements

Write code to get substring Before separator

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String str = "book2s.com";
        String separator = ".";
        System.out.println(substringBefore(str, separator));
    }//from   w  w  w .  j av  a2 s . com

    public static String substringBefore(String str, String separator) {
        if (isEmpty(str) || separator == null)
            return str;
        if (separator.length() == 0)
            return "";
        int pos = str.indexOf(separator);
        if (pos == -1)
            return str;
        else
            return str.substring(0, pos);
    }

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

    public static int indexOf(String str, char searchChar, int startPos) {
        if (isEmpty(str))
            return -1;
        else
            return str.indexOf(searchChar, 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 "";
        else
            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 Exercise