fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.nio.file.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. String result = transform("/some/path/to/some/dir");
  13. System.out.println(result);
  14. }
  15.  
  16. private static String transform(String p) {
  17. Path path = Paths.get(p);
  18. int nameCount = path.getNameCount();
  19. if (nameCount < 2) throw new RuntimeException();
  20. return String.format("%s_%s", path.getName(nameCount-2), path.getName(nameCount-1));
  21. }
  22. }
Success #stdin #stdout 0.05s 2188288KB
stdin
Standard input is empty
stdout
some_dir