Regex version (simplest):
String text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.";
int lineMaxLength = 10;
System.out.println(java.util.Arrays.toString(
text.split("(?<=\\G.{"+lineMaxLength+"})")
));
Which prints:
[Lorem Ipsu, m is simpl, y dummy te, xt of the , printing a, nd typeset, ting indus, try. Lorem, Ipsum has, been the , industry's, standard , dummy text, ever sinc, e the 1500, s, when an, unknown p, rinter too, k a galley, of type a, nd scrambl, ed it to m, ake a type, specimen , book.]
Without regex:
import java.util.List;
import java.util.ArrayList;
public class MyClass {
public static void main(String args[]) {
String text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.";
int lineMaxLength = 10;
List<String> lines = new ArrayList<>();
int length = text.length();
StringBuilder s = new StringBuilder();
for(int i = 0; i < length; i++){
if (i % lineMaxLength == 0){
if(i != 0){
lines.add(s.toString());
}
s = new StringBuilder();
}
s.append(text.charAt(i));
}
int linesLength = lines.size();
for(int i= 0; i < linesLength; i++){
System.out.println(lines.get(i));
}
}
}
Which prints:
Lorem Ipsu
m is simpl
y dummy te
xt of the
printing a
nd typeset
ting indus
try. Lorem
Ipsum has
been the
industry's
standard
dummy text
ever sinc
e the 1500
s, when an
unknown p
rinter too
k a galley
of type a
nd scrambl
ed it to m
ake a type
specimen