Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java code for wrapping text lines to a max line width

Before I re-invent the wheel (poorly), I'd like to know if there is a some existing Java code for wrapping text lines to a given maximum width. Ideally it would:

  • respect existing linebreaks
  • break up lines that exceed a maximum length on word boundaries
  • break up words whose length exceeds the maximum line width by inserting hyphens

Edit: there are no "pixels" here, only java.lang.String. "maximum width" refers to the number of characters on a line.

like image 749
George Armhold Avatar asked Oct 29 '10 19:10

George Armhold


People also ask

How do you wrap text in Java?

Java For Testers As a solution you can wrap the text within the width of the window by setting the value to the property wrapping with, using the setWrappingWidth() method. This method accepts a double value representing the width (in pixels) of the text.

How do you wrap a sentence in Java?

To wrap the lines of JTextArea we need to call the setLineWrap(boolean wrap) method and pass a true boolean value as the parameter. The setWrapStyleWord(boolean word) method wrap the lines at word boundaries when we set it to true .

What is line wrap code?

The Line Wrap feature is now available in the desktop version of Google Chrome and Microsoft Edge browser. It provides you the ability to see the source code for any web page you want. In other words, you can say this feature allows to use of the line wrap option while inspecting the source code of a web page.


3 Answers

Apache commons has WordUtils and wrap function in it:

http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/text/WordUtils.html

P.S. Looks like this is deprecated and you need to use

https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/WordUtils.html

instead.

like image 64
Roman Goyenko Avatar answered Sep 18 '22 05:09

Roman Goyenko


Use the word-wrap library (available on Maven Central).

Here's one way to use it:

String text = "hi there how are you going?";
String wrapped = 
  WordWrap.from(text)
    .maxWidth(10)
    .insertHyphens(true) // true is the default
    .wrap();

Output is:

hi there
how are
you going?

The library conserves leading spaces on lines which is one complaint about the behaviour of the Apache commons-lang offering. You can also specify the stringWidth function to get pixel-accurate results when rendering the text.

The library has decent unit test coverage (something to bear in mind when you consider copy and paste of code chunks from the web!).

The Maven dependency is:

<dependency>
  <groupId>com.github.davidmoten</groupId>
  <artifactId>word-wrap</artifactId>
  <version>0.1.9</version>
</dependency>

Be sure to check for a later version.

like image 28
Dave Moten Avatar answered Sep 22 '22 05:09

Dave Moten


Here's my take

private static final String LINEBREAK = "\n"; // or "\r\n";

public static String wrap(String string, int lineLength) {
    StringBuilder b = new StringBuilder();
    for (String line : string.split(Pattern.quote(LINEBREAK))) {
        b.append(wrapLine(line, lineLength));
    }
    return b.toString();
}

private static String wrapLine(String line, int lineLength) {
    if (line.length() == 0) return LINEBREAK;
    if (line.length() <= lineLength) return line + LINEBREAK;
    String[] words = line.split(" ");
    StringBuilder allLines = new StringBuilder();
    StringBuilder trimmedLine = new StringBuilder();
    for (String word : words) {
        if (trimmedLine.length() + 1 + word.length() <= lineLength) {
            trimmedLine.append(word).append(" ");
        } else {
            allLines.append(trimmedLine).append(LINEBREAK);
            trimmedLine = new StringBuilder();
            trimmedLine.append(word).append(" ");
        }
    }
    if (trimmedLine.length() > 0) {
        allLines.append(trimmedLine);
    }
    allLines.append(linebreak);
    return allLines.toString();
}

(This solution strips two spaces to one space (so same fault that @jett has with Apache commons WordUtils)).

like image 39
Esko Piirainen Avatar answered Sep 18 '22 05:09

Esko Piirainen