Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    public static String getLastPath(String path) {
        String strippedPath = removeLastPath(path);

        if (strippedPath == null) {
            return null;
        }

        if (strippedPath.equals(path)) {
            return null;
        } else {
            String result = path.substring(strippedPath.length() + 1);

            if (result.endsWith("/")) {
                result = result.substring(0, result.length() - 1);
            }

            return result;
        }
    }

    public static String removeLastPath(String path) {
        String result = path;

        if (result == null) {
            return result;
        }

        if (!result.isEmpty() && result.charAt(result.length() - 1) == '/') {
            result = result.substring(0, result.length() - 2);
        }

        if (!result.contains("/")) {
            return result;
        } else {
            return result.substring(0, result.lastIndexOf("/"));
        }

    }
}