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  w w  .j a v  a2 s .com

    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