suppose the string is like this:
String msg = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
I want to add or append a line break (a carriage return) after every 60 characters either through looping or regEX (regEx would be much cooler).
解决方案
You could do something like so in java :
String msg = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz...";
String updatedMsg = msg.replaceAll("(.{60})", "$1\r");
This will replace every 60 characters with the same 60 characters and add a carriage return at the end.
The (.{60}) will capture a group of 60 characters. The $1 in the second will put the content of the group. The \r will be then appended to the 60 characters which have been just matched. Have a look at http://www.regular-expressions.info/java.html