String (@NotNull String path, int slashIndex) { StringBuilder s = new StringBuilder(path.length()); s.append(path, 0, slashIndex); for (int i = slashIndex; i < path.length(); i++) { char c = path.charAt(i); if (c == '/') { char nextC = i == path.length()-1 ? 0 : path.charAt(i + 1); if (nextC == '.') { if (i == path.length() - 2) { // ends with "/.", ignore break; } char nextNextC = path.charAt(i + 2); if (nextNextC == '/') { i++; // "/./" in the middle, ignore "/." continue; } // "/.xxx", append } else if (nextC == '/') { // ignore duplicate / continue; } } s.append(c); } return s.toString(); }