Java Utililty Methods Path String Clean

List of utility methods to do Path String Clean

Description

The list of methods to do Path String Clean are organized into topic(s).

Method

StringcleanPath(final String path)
clean Path
String trimmed = path.trim();
if (!trimmed.startsWith("/"))
    trimmed = "/" + trimmed;
if (trimmed.endsWith("/"))
    trimmed = trimmed.substring(0, trimmed.length() - 1); 
return trimmed;
StringcleanPath(final String url)
Clean repeated // characters from the URL path.
return url.replaceAll("/+", "/").replaceAll("^((\\w)+:)/", "$1//").replaceAll("^file://", "file:///");
StringcleanPath(String loc)
clean Path
if (loc.startsWith("reference:file:")) {
    loc = loc.substring(15);
} else if (loc.startsWith("file:")) {
    loc = loc.substring(5);
} else {
    return loc;
loc = loc.replace("//", "/");
...
StringcleanPath(String path)
clean path, remove double "/" and replace "\" by "/"
if (path == null) {
    return null;
} else {
    path = path.replace('\\', '/');
    while (path.indexOf("//") >= 0) {
        path = path.replace("//", "/");
    return path;
...
StringcleanPath(String path)
clean Path
if (path.endsWith("/")) {
    path = path.replaceAll("/+$", "");
if (path.startsWith("/")) {
    path = path.replaceAll("^/+", "");
return path.replaceAll("/+", "/");
StringcleanPath(String path)
replace questionable chars in path with percent representation
String s;
s = path.replaceAll("#", "%23");
s = s.replaceAll(":", "%3A");
return s;
StringcleanPath(String path)
Make sure the file separator is at the end of a given path.
if (!path.endsWith(System.getProperty("file.separator"))) {
    path = path.concat(System.getProperty("file.separator"));
return path;
StringcleanPath(String path)
Clean the path string by removing leading and trailing slashes and removing duplicate slashes.
if (path.endsWith(SEPARATOR)) {
    path = path.replaceAll(SEPARATOR + "+$", "");
if (path.startsWith(SEPARATOR)) {
    path = path.replaceAll("^" + SEPARATOR + "+", "");
return path.replaceAll("/+", SEPARATOR);
StringcleanPath(String path)
Removes a trailing path separator.
String slash = System.getProperty("file.separator");
if (slash != null && slash.length() > 0) {
    int len = path.length();
    if (len > 0 && path.charAt(len - 1) == slash.charAt(0)) {
        return path.substring(0, len - 1);
return path;
...
StringcleanPath(String path)
clean Path
while (path.endsWith("/") && path.length() > 1) {
    path = path.substring(0, path.length() - 1);
return path;