Java Utililty Methods Path Normalize

List of utility methods to do Path Normalize

Description

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

Method

StringnormalizeRegex(String pathPattern)
Remove trailing regex path parts
int[] offsets = offsets(pathPattern);
int cursor = 0;
int ch = pathPattern.codePointAt(cursor);
for (;;) {
    switch (ch) {
    case '*':
    case '+':
    case '?':
...
StringnormalizeResourcesPath(final String path)
normalize Resources Path
if (path == null) {
    throw new IllegalArgumentException("Path must not be null.");
String result = path.replace('\\', '/');
if (result.startsWith("/")) {
    if (result.length() == 1) {
        result = "";
    } else {
...
StringnormalizeRootPath(String rootPath)
normalize Root Path
if (isNullOrEmpty(rootPath)) {
    return null;
if (!rootPath.startsWith(PATH_SEPARATOR)) {
    rootPath = PATH_SEPARATOR + rootPath;
if (!rootPath.endsWith(PATH_SEPARATOR)) {
    rootPath = rootPath + PATH_SEPARATOR;
...
StringnormalizeSlashes(final String path)
Adds a '/' prefix to the beginning of a path if one isn't present and removes trailing slashes if any are present.
final StringBuilder builder = new StringBuilder(path);
boolean modified = false;
while (builder.length() > 0 && builder.length() != 1
        && PATH_SEPARATOR == builder.charAt(builder.length() - 1)) {
    builder.deleteCharAt(builder.length() - 1);
    modified = true;
if (builder.length() == 0 || PATH_SEPARATOR != builder.charAt(0)) {
...
StringnormalizeSlashes(String path)
Replace '\' with '/' from the given path because tsserver normalize it like this.
return path.replaceAll("\\\\", "/");
StringBuildernormalizeToDir(CharSequence path)
If: We're at the top of the tree don't remove parent file name.
StringBuilder results = new StringBuilder(path);
if (!isDirectory(results.toString())) {
    int lastDir = results.lastIndexOf("/");
    if (lastDir < 0) {
        results = new StringBuilder("/");
    } else {
        results.replace(lastDir, path.length(), "/");
return results;
StringnormalizeURI(String prefix, String relativePath)
Returns the (smart) concatenation of the prefix and the relativePath.
if (prefix == null)
    return relativePath;
if (relativePath == null)
    return prefix;
if (relativePath.startsWith(prefix)) 
    return relativePath;
if (!relativePath.startsWith("//")) {
    if (relativePath.startsWith("/")) 
...
StringnormalizeUriPath(String path)
normalize Uri Path
if (path == null) {
    return "";
if (path.isEmpty()) {
    return path;
if (path.endsWith(URI_PATH_CHAR)) {
    path = path.substring(0, path.length() - 1);
...
StringnormalizeUrlPath(String name)
normalize Url Path
if (name.startsWith("/")) {
    name = name.substring(1);
int i = name.indexOf("/..");
if (i > 0) {
    int j = name.lastIndexOf("/", i - 1);
    if (j >= 0) {
        name = name.substring(0, j) + name.substring(i + 3);
...
StringnormalizeUrlPath(String protocol, String host, int portNumber, String basePath)
normalize Url Path
host = protocol + "://" + host;
String port = portNumber == 80 ? "" : ":" + portNumber;
String path = basePath;
path = path.startsWith("/") || path.isEmpty() ? path : "/" + path;
String url = host + port + path;
url = url.substring(0, 7) + url.substring(7).replaceAll("//", "/");
return url;