Java - Write code to get Before Separator Or Nothing

Requirements

Write code to get Before Separator Or Nothing

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String name = "book2s.com";
        System.out.println(getBeforeSeparatorOrNothing(name));
    }/*w ww . ja  v  a  2  s . c o  m*/

    private static final String SEPARATOR = ":";

    public static String getBeforeSeparatorOrNothing(String name) {
        int index = name.indexOf(SEPARATOR);
        if (index == -1 || name.startsWith(SEPARATOR))
            return "";
        else
            return name.substring(0, index);
    }
}

Related Exercise