org.apache.commons.text.WordUtils.wrap() Java Code Examples

How to use wrap() method in org.apache.commons.text.WordUtils class.

More Java code examples for org.apache.commons.text.WordUtils Apache Commons Text library.

Java Code Example 1 wraps a single line of text to multiple lines

import org.apache.commons.text.WordUtils;

public class WordUtilsWrapExample1 {
    public static void main(String[] args) {
        String inputString = "This is the line of text need to be wrap.";

        String wrappedString = WordUtils.wrap(inputString, 20);

        System.out.println("Input String: ");
        System.out.println(inputString);
        System.out.println("Output String:");
        System.out.println(wrappedString);
    }
}
Output:

Input String: 
This is the line of text need to be wrap.
Output String:
This is the line of
text need to be
wrap.

Java Code Example 2 wraps a single line of text to multiple lines

import org.apache.commons.text.WordUtils;

public class WordUtilsWrapExample2 {
    public static void main(String[] args) {
        String inputString = "Click here to go to the Simple Solution website - https://simplesolution.dev/";
        int wrapLength = 20;
        String newLineString = "\n";
        boolean isWrapLongWords = true;

        String wrappedString = WordUtils.wrap(inputString, wrapLength, newLineString, isWrapLongWords);

        System.out.println("Input String: ");
        System.out.println(inputString);
        System.out.println("Output String:");
        System.out.println(wrappedString);
    }
}
Output:

Input String: 
Click here to go to the Simple Solution website - https://simplesolution.dev/
Output String:
Click here to go to
the Simple Solution
website -
https://simplesoluti
on.dev/

Java Code Example 3 wraps a single line of text to multiple lines

import org.apache.commons.text.WordUtils;

public class WordUtilsWrapExample3 {
    public static void main(String[] args) {
        String inputString = "Click here to go to the Simple Solution website - https://simplesolution.dev/";
        int wrapLength = 20;
        String newLineString = "\n";
        boolean isWrapLongWords = false;

        String wrappedString = WordUtils.wrap(inputString, wrapLength, newLineString, isWrapLongWords);

        System.out.println("Input String: ");
        System.out.println(inputString);
        System.out.println("Output String:");
        System.out.println(wrappedString);
    }
}
Output:

Input String: 
Click here to go to the Simple Solution website - https://simplesolution.dev/
Output String:
Click here to go to
the Simple Solution
website -
https://simplesolution.dev/

Wrap a line of text into multiple lines in Java using Apache Commons Text