fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  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 key = "A-B-C-D"; // => [A, B, C, D]
  13. String key = "A-B-C-D-E-F"; // => [A_B, C, D, E, F]
  14. int keep = 3;
  15. String[] res = key.split("-");
  16. if (res.length > 4) {
  17. String first = String.join("-", Arrays.asList(res).subList(0, keep));
  18. List<String> lst = new ArrayList<>();
  19. lst.add(first);
  20. lst.addAll(Arrays.asList(res).subList(keep, res.length));
  21. res = new String[lst.size()];
  22. res = lst.toArray(res);
  23. }
  24. System.out.println(Arrays.toString(res));
  25. }
  26. }
Success #stdin #stdout 0.1s 321600KB
stdin
Standard input is empty
stdout
[A-B-C, D, E, F]