Java - parse String and add Line break every 100 characters

java, regex

Solution

You could do something like so:

String str = "....";
String parsedStr = str.replaceAll("(.{100})", "$1\n");

This will replace every 100 characters with the same 100 characters and add a new line at the end.

The `(.{100})` will capture a group of 100 characters. The `$1` in the second will put the content of the group. The `\n` will be then appended to the 100 characters which have been just matched.

Problem

I wanted to ask how to parse a String and add a Line break (`\n`) every 100 characters. I know that you can parse The String with Regex, but don't know how to proceed later on. Can somebody help?

Original source

Related problems