Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Large string split into lines with maximum length in java

String input = "THESE TERMS AND CONDITIONS OF SERVICE (the Terms) ARE A LEGAL AND BINDING AGREEMENT BETWEEN YOU AND NATIONAL GEOGRAPHIC governing your use of this site, www.nationalgeographic.com, which includes but is not limited to products, software and services offered by way of the website such as the Video Player, Uploader, and other applications that link to these Terms (the Site). Please review the Terms fully before you continue to use the Site. By using the Site, you agree to be bound by the Terms. You shall also be subject to any additional terms posted with respect to individual sections of the Site. Please review our Privacy Policy, which also governs your use of the Site, to understand our practices. If you do not agree, please discontinue using the Site. National Geographic reserves the right to change the Terms at any time without prior notice. Your continued access or use of the Site after such changes indicates your acceptance of the Terms as modified. It is your responsibility to review the Terms regularly. The Terms were last updated on 18 July 2011.";  //text copied from http://www.nationalgeographic.com/community/terms/ 

I want to split this large string into lines and the lines should not content more than MAX_LINE_LENGTH characters in each line.

What I tried so far

int MAX_LINE_LENGTH = 20;     System.out.print(Arrays.toString(input.split("(?<=\\G.{MAX_LINE_LENGTH})"))); //maximum length of line 20 characters 

Output :

[THESE TERMS AND COND, ITIONS OF SERVICE (t, he Terms) ARE A LEGA, L AND B ... 

It causes breaking of words. I don't want this. Instead of I want to get output like this:

[THESE TERMS AND , CONDITIONS OF , SERVICE (the Terms) , ARE A LEGAL AND B ... 

One more condition added : If a word length is greater than MAX_LINE_LENGTH then the word should get split.

And solution should be without helping of external jars.

like image 597
Abhishek Avatar asked Sep 23 '11 11:09

Abhishek


People also ask

What is the maximum possible length of an string in Java?

String is considered as char array internally,So indexing is done within the maximum range. This means we cannot index the 2147483648th member.So the maximum length of String in java is 2147483647.

What is string split limit?

A positive limit - The String will be split up to a maximum of limit - 1 times. Beyond this, the rest of the string will be returned as the last element of the array, as it is, without splitting.

How can you get the length of a string's in Java?

To calculate the length of a string in Java, you can use an inbuilt length() method of the Java string class. In Java, strings are objects created using the string class and the length() method is a public member method of this class. So, any variable of type string can access this method using the . (dot) operator.


2 Answers

Just iterate through the string word by word and break whenever a word passes the limit.

public String addLinebreaks(String input, int maxLineLength) {     StringTokenizer tok = new StringTokenizer(input, " ");     StringBuilder output = new StringBuilder(input.length());     int lineLen = 0;     while (tok.hasMoreTokens()) {         String word = tok.nextToken();          if (lineLen + word.length() > maxLineLength) {             output.append("\n");             lineLen = 0;         }         output.append(word);         lineLen += word.length();     }     return output.toString(); } 

I just typed that in freehand, you may have to push and prod a bit to make it compile.

Bug: if a word in the input is longer than maxLineLength it will be appended to the current line instead of on a too-long line of its own. I assume your line length is something like 80 or 120 characters, in which case this is unlikely to be a problem.

like image 193
Barend Avatar answered Sep 28 '22 10:09

Barend


Best : use Apache Commons Lang :

org.apache.commons.lang.WordUtils

/**  * <p>Wraps a single line of text, identifying words by <code>' '</code>.</p>  *   * <p>New lines will be separated by the system property line separator.  * Very long words, such as URLs will <i>not</i> be wrapped.</p>  *   * <p>Leading spaces on a new line are stripped.  * Trailing spaces are not stripped.</p>  *  * <pre>  * WordUtils.wrap(null, *) = null  * WordUtils.wrap("", *) = ""  * </pre>  *  * @param str  the String to be word wrapped, may be null  * @param wrapLength  the column to wrap the words at, less than 1 is treated as 1  * @return a line with newlines inserted, <code>null</code> if null input  */ public static String wrap(String str, int wrapLength) {     return wrap(str, wrapLength, null, false); } 
like image 41
Saad Benbouzid Avatar answered Sep 28 '22 08:09

Saad Benbouzid