Java Path Normalize normalizePath(String path)

Here you can find the source of normalizePath(String path)

Description

normalize Path

License

Open Source License

Declaration

public static String normalizePath(String path) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static String normalizePath(String path) {
        char[] array = path.toCharArray();
        StringBuffer sb = new StringBuffer();

        boolean skipNextPathChar = false;
        int idx = 0;

        for (int i = 0; i < array.length; i++) {
            char c = array[i];
            boolean isPathChar = isPathChar(c);
            if (isPathChar) {
                if (skipNextPathChar)
                    continue;

                skipNextPathChar = true;
            } else {
                skipNextPathChar = false;
            }/*w  w  w.ja  v a2s  .c  om*/

            idx++;
            sb.append(c);
        }

        if (idx > 1) {
            char last = sb.charAt(idx - 1);
            if (isPathChar(last)) {
                return sb.substring(0, idx - 1);
            }
        }

        return sb.toString();
    }

    public static boolean isPathChar(char c) {
        return c == '/' || c == '.';
    }
}

Related

  1. normalizePath(String path)
  2. normalizePath(String path)
  3. normalizePath(String path)
  4. normalizePath(String path)
  5. normalizePath(String path)
  6. normalizePath(String path)
  7. normalizePath(String path)
  8. normalizePath(String path)
  9. normalizePath(String path)