I have a string:
String testString= "For the time being, programming is a consumer job, assembly line coding is the norm, and what little exciting stuff is being performed is not going to make it compared to the mass-marketed cräp sold by those who think they can surf on the previous half-century's worth of inventions forever"
like this: For the time being, programmi \n........\n.......\n
After each length of 20 characters in this string, I want to put a newline character \n for display in a TextView in Android.
You must have to use regex for achieve your task its fast and efficient. Try below code:-
String str = "....";
String parsedStr = str.replaceAll("(.{20})", "$1\n");
The (.{20}) will capture a group of 20 characters. The $1 in the second will put the content of the group. The \n will be then appended to the 20 characters which have been just matched.
How about something like that?
String s = "...whateverstring...";
for(int i = 0; i < s.length(); i += 20) {
s = new StringBuffer(s).insert(i, "\n").toString();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With