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 w w  w  . ja v  a 2 s  .c  o m*/

    /**
     * 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