fork(2) download
  1. import java.util.*;
  2. import java.util.regex.*;
  3. import java.lang.*;
  4.  
  5. public class Main {
  6. public static String wrap(String s, int max) {
  7. Matcher m = Pattern.compile("(.{1," + max + "}(\\W|$))").matcher(s);
  8. StringBuilder b = new StringBuilder();
  9. while (m.find()) {
  10. b.append(m.group()).append("\n");
  11. }
  12. return b.toString();
  13. }
  14. public static void main(String[] args) {
  15. System.out.println(wrap("Hello, my name is Patrick and I have a lot to say, but I will tell you that another day.", 20));
  16. System.out.println("------");
  17. System.out.println(wrap("HHello, how are you doooing misteer", 20));
  18. }
  19.  
  20. }
Success #stdin #stdout 0.06s 215552KB
stdin
Standard input is empty
stdout
Hello, my name is 
Patrick and I have a 
lot to say, but I 
will tell you that 
another day.

------
HHello, how are you 
doooing misteer