Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrap the string after a number of characters word-wise in Java

Tags:

I have this code:

    String s = "A very long string containing " +
                   "many many words and characters. " +
                   "Newlines will be entered at spaces.";

    StringBuilder sb = new StringBuilder(s);

    int i = 0;
    while ((i = sb.indexOf(" ", i + 20)) != -1) {
        sb.replace(i, i + 1, "\n");
    }

    System.out.println(sb.toString());

The output of the code is:

A very long string containing
many many words and
characters. Newlines
will be entered at spaces.

The above code is wrapping the string after the next space of every 30 characters, but I need to wrap the string after the previous space of every 30 characters, like for the first line it will be:

A very long string

And the 2nd line will be

containing many

Please give some proper solution.

like image 995
Suvonkar Avatar asked Nov 18 '10 07:11

Suvonkar


People also ask

How do you wrap text in a string in Java?

String s = "A very long string containing " + "many many words and characters. " + "Newlines will be entered at spaces."; StringBuilder sb = new StringBuilder(s); int i = 0; while ((i = sb. indexOf(" ", i + 20)) != -1) { sb. replace(i, i + 1, "\n"); } System.

How do you wrap char in Java?

A character array can be wrapped into a buffer using the method wrap() in the class java. nio. CharBuffer. This method requires a single parameter i.e. the char array to be wrapped into a buffer and it returns the new buffer created.

How do I return the number of letters in a string in Java?

Java has an inbuilt method called length() to find the number of characters of any String. int length(); where length() is a method to find the number of characters and returns the result as an integer.

How do I wrap a string in Java?

The first wrap () method takes the String to wrap as its first argument and the wrap length as its second argument. The second wrap () method takes for arguments. The first is the String to wrap and the second is the wrap length.

What happens to the last character of a string in Java?

Given string str, the task is to write a Java program to swap the pairs of characters of a string. If the string contains an odd number of characters then the last character remains as it is. Explanation: The given string contains even number of characters.

How do I wrap a string in the Commons Lang s library?

The WordUtils utility class in the Commons Lang S library has two methods for performing word wrapping given a String. The first wrap () method takes the String to wrap as its first argument and the wrap length as its second argument. The second wrap () method takes for arguments. The first is the String to wrap and the second is the wrap length.

How to swap a pair of characters in a string?

Get the string to swap a pair of characters. Check if the string is null or empty then return the string. Converting the given string into a character array. Traverse the character array and swap the characters. Now, print the result.


3 Answers

You can use Apache-common's WordUtils.wrap().

like image 150
Emil Avatar answered Sep 24 '22 06:09

Emil


Use lastIndexOf instead of indexOf, e.g.

StringBuilder sb = new StringBuilder(s);

int i = 0;
while (i + 20 < sb.length() && (i = sb.lastIndexOf(" ", i + 20)) != -1) {
    sb.replace(i, i + 1, "\n");
}

System.out.println(sb.toString());

This will produce the following output:

A very long string
containing many
many words and
characters.
Newlines will be
entered at spaces.
like image 36
vitaut Avatar answered Sep 26 '22 06:09

vitaut


You can try the following:

public static String wrapString(String s, String deliminator, int length) {
    String result = "";
    int lastdelimPos = 0;
    for (String token : s.split(" ", -1)) {
        if (result.length() - lastdelimPos + token.length() > length) {
            result = result + deliminator + token;
            lastdelimPos = result.length() + 1;
        }
        else {
            result += (result.isEmpty() ? "" : " ") + token;
        }
    }
    return result;
}

call as wrapString("asd xyz afz","\n",5)

like image 2
Yalovali Avatar answered Sep 24 '22 06:09

Yalovali