Appends the supplied affix to the specified source path, ensuring that exactly one path separator (/ as we are dealing with URLs here not platform specific file-system paths) is used between the two. - Java File Path IO

Java examples for File Path IO:Path

Description

Appends the supplied affix to the specified source path, ensuring that exactly one path separator (/ as we are dealing with URLs here not platform specific file-system paths) is used between the two.

Demo Code

// Copyright (C) 2001-2012 Michael Bayne, et al.
//package com.java2s;

public class Main {
    /**//from   w  ww . ja va2 s  . c  om
     * Appends the supplied affix to the specified source path, ensuring
     * that exactly one path separator (<code>/</code> as we are dealing
     * with URLs here not platform specific file-system paths) is used
     * between the two. <em>Note:</em> this means that the affix will be
     * made into a relative path regardless of whether or not it starts
     * with a <code>/</code>.
     */
    public static String appendPath(String source, String affix) {
        if (source.endsWith("/")) {
            if (affix.startsWith("/")) {
                return source + affix.substring(1);
            } else {
                return source + affix;
            }
        } else if (affix.startsWith("/")) {
            return source + affix;
        } else {
            return source + "/" + affix;
        }
    }
}

Related Tutorials