fork download
  1. class Main {
  2. public static void main(String[] args) {
  3. System.out.println(breakString("That little pup treating you alright? I bet he'll grow strong if you give it lots of love!", 42));
  4. System.out.println("-----------------");
  5. System.out.println(breakString("Boosts the power of a move that's used repeatedly. Once the chain is broken, the move's power returns to normal.", 23));
  6. System.out.println("-----------------");
  7. System.out.println(breakString("A bizarre orb that gives off heat when touched and will afflict the holder with a burn during battle.", 23));
  8. System.out.println("-----------------");
  9. System.out.println(breakString("This headband exudes strength, slightly boosting the power of the holder's physical moves.", 23));
  10. System.out.println("-----------------");
  11. System.out.println(breakString("This herb will allow the holder to mirror an opponent's stat increases to boost its own stats - but only once.", 23));
  12. System.out.println("-----------------");
  13. System.out.println(breakString("This water can be crossed!\n(You need 4 badges to use Surf outside of battle!)", 42));
  14. }
  15.  
  16. public static String breakString(String input, int maxChar) {
  17. if (input == null || maxChar <= 0) {
  18. return null;
  19. }
  20.  
  21. StringBuilder result = new StringBuilder();
  22. StringBuilder currentLine = new StringBuilder();
  23. int currentLength = 0;
  24.  
  25. // split on spaces and tabs but not \n
  26. for (String word : input.split("[ \\t]+")) {
  27. // split the word if it contains \n
  28. String[] parts = word.split("\n", -1);
  29.  
  30. for (int i = 0; i < parts.length; i++) {
  31. String part = parts[i];
  32.  
  33. // check if need to wrap before adding this part
  34. if (currentLength + part.length() > maxChar) {
  35. result.append(currentLine.toString().trim()).append("\n");
  36. currentLine.setLength(0);
  37. currentLength = 0;
  38. }
  39.  
  40. currentLine.append(part);
  41. currentLength += part.length();
  42.  
  43. // if this part was followed by a \n break the line
  44. if (i < parts.length - 1) {
  45. result.append(currentLine.toString().trim()).append("\n");
  46. currentLine.setLength(0);
  47. currentLength = 0;
  48. } else {
  49. currentLine.append(" ");
  50. currentLength += 1;
  51. }
  52. }
  53. }
  54.  
  55. // append any leftover line
  56. if (currentLine.length() > 0) {
  57. result.append(currentLine.toString().trim());
  58. }
  59.  
  60. return result.toString();
  61. }
  62. }
Success #stdin #stdout 0.08s 55500KB
stdin
Standard input is empty
stdout
That little pup treating you alright? I
bet he'll grow strong if you give it lots
of love!
-----------------
Boosts the power of a
move that's used
repeatedly. Once the
chain is broken, the
move's power returns to
normal.
-----------------
A bizarre orb that
gives off heat when
touched and will
afflict the holder with
a burn during battle.
-----------------
This headband exudes
strength, slightly
boosting the power of
the holder's physical
moves.
-----------------
This herb will allow
the holder to mirror an
opponent's stat
increases to boost its
own stats - but only
once.
-----------------
This water can be crossed!
(You need 4 badges to use Surf outside of
battle!)