Java - Write code to get Extension from File path String

Requirements

Write code to get Extension from File path String

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String path = "c://a//asdf/asdf/as/df/asd/f/asd/f/book2s.com";
        System.out.println(getExtension(path));
    }//  w w  w .  j  a  v  a2 s .c o  m

    public static String getExtension(String path) {
        String[] split = path.split("\\.");
        if (split.length > 1) {
            String ext = split[split.length - 1];
            if (ext.length() <= 4) {
                return ext;
            }
        }
        return "";
    }
}

Related Exercise