code: public static String stringByDeletingLastPathComponent(final String str) {
          String path = str;
          int start = str.length() - 1;
          while (path.charAt(start) == '/') { start--; }
  
          final int index = path.lastIndexOf('/', start);
          path = (index < 0) ? "" : path.substring(0, index);
  
  
          if (path.length() == 0 && str.charAt(0) == '/') { return "/"; }
  
          return path;
      }
docs: https://developer.apple.com/reference/foundation/nsstring/1411141-stringbydeletinglastpathcomponen