public static String subtractPaths ( String path , String prefix ) throws InvalidPathException { String cleanedPath = cleanPath ( path ) ; String cleanedPrefix = cleanPath ( prefix ) ; if ( cleanedPath . equals ( cleanedPrefix ) ) { return "" ; } if ( ! hasPrefix ( cleanedPath , cleanedPrefix ) ) { throw new RuntimeException ( String . format ( "Cannot subtract %s from %s because it is not a prefix" , prefix , path ) ) ; } // The only clean path which ends in '/' is the root. int prefixLen = cleanedPrefix . length ( ) ; int charsToDrop = PathUtils . isRoot ( cleanedPrefix ) ? prefixLen : prefixLen + 1 ; return cleanedPath . substring ( charsToDrop , cleanedPath . length ( ) ) ; }
Removes the prefix from the path yielding a relative path from the second path to the first .