在String for Java中每60个字符后添加换行符 [英] Add line break after every 60 characters in String for Java
问题描述
假设字符串是这样的:
String msg = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
我想通过循环或regEX在每60个字符后添加或附加换行符(回车符)(regEx会更冷)。
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).
推荐答案
你可以在java中做这样的事情:
You could do something like so in java :
String msg = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz...";
String updatedMsg = msg.replaceAll("(.{60})", "$1\r");
这将用相同的60个字符替换每60个字符,并在末尾添加回车符。
This will replace every 60 characters with the same 60 characters and add a carriage return at the end.
(。{60})将捕获一组60个字符。第二个1美元将放置该组的内容。然后将\r附加到刚刚匹配的60个字符。请查看 http://www.regular-expressions.info/java.html
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
这篇关于在String for Java中每60个字符后添加换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!