Java - Write code to Returns the substring after the last dot ('.').

Requirements

Write code to Returns the substring after the last dot ('.').

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String string = "book2s.com";
        System.out.println(substringAfterLastDot(string));
    }/*from  ww  w .j  a v a2 s  . c  om*/

    /**
     * Returns the substring after the last dot ('.').
     * "bar" = substringAfterLastDot("my.foo.bar");
     * 
     * @param string
     * @return The substring after the last dot.
     */
    public static String substringAfterLastDot(String string) {
        return getSubstringAfter(string, '.');
    }

    public static String getSubstringAfter(String s, char c) {
        return s.substring(s.lastIndexOf(c) + 1).trim();
    }
}

Related Exercise