Reputation: 31
I am new to Java and want to split a message that a user inputs into multiple lines depending on what has been inputted as the maximum length. How would I go about repeating it? Here is what I have so far:
int rem = m - maxlength;
System.out.println(message.substring(0, message.length() - rem));
System.out.println(message.substring(message.length() - rem));
Upvotes: 2
Views: 1570
Reputation: 109
A little bit of improved @markspace code to get the last words too (matcher does not get the last words because of the end of the line):
public static void main( String[] args ) {
String s = "I am new to Java and want to split a message that a user inputs into multiple lines depending on what has been inputted as the maximum length. How would I go about repeating it? Here is what I have so far:";
String regex = ".{1,24}(\\s|$)";
Matcher m = Pattern.compile(regex).matcher(s);
while (m.find()) {
System.out.println(m.group());
}
Upvotes: 0
Reputation: 33
int maxlength = 10;
String message = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String transformedMessage = message.replaceAll("(?<=\\G.{" + maxlength + "})", "\n");
System.out.println(transformedMessage);
/*Output
1234567890
ABCDEFGHIJ
KLMNOPQRST
UVWXYZ
*/
Upvotes: 0
Reputation: 18408
Use WordIterator like this. BreakIterator classes are locale sensitive. You can use with different languages.
public static void main(String[] args) {
String msg = "This is a long message. Message is too long. Long is the message";
int widthLimit = 15;
printWithLimitedWidth(msg, widthLimit);
}
static void printWithLimitedWidth(String s, int limit) {
BreakIterator br = BreakIterator.getWordInstance(); //you can get locale specific instance to handle different languages.
br.setText(s);
int currLenght = 0;
int start = br.first();
int end = br.next();
while (end != BreakIterator.DONE) {
String word = s.substring(start,end);
currLenght += word.length();
if (currLenght <= limit) {
System.out.print(word);
} else {
currLenght = 0;
System.out.print("\n"+word);
}
start = end;
end = br.next();
}
}
Output:
This is a long
message. Message is
too long. Long is
the message
This is a sample code. Handle white space and other special characters according to your needs. For more info refer https://docs.oracle.com/javase/tutorial/i18n/text/about.html
Upvotes: 2
Reputation: 11030
This sort of works. I picked a very short "line" length of 10 characters, you should probably increase this. But it shows how you might do this without too much code. Cheange the "10" in the regex to a different number to increase the line length.
(I changed the code a bit to make it more obvious that this method does not split words in the middle but always splits at a white space.)
public static void main( String[] args ) {
String s = "This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. ";
String regex = ".{1,10}\\s";
Matcher m = Pattern.compile( regex ).matcher( s );
ArrayList<String> lines = new ArrayList<>();
while( m.find() ) {
lines.add( m.group() );
}
System.out.println( String.join( "\n", lines ) );
}
run:
This is a
test. This
is a test.
This is a
test. This
is a test.
This is a
test. This
is a test.
BUILD SUCCESSFUL (total time: 0 seconds)
Upvotes: 1
Reputation: 31992
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
Upvotes: 1