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.
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.
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.
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.
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.
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.
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.
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.
You can use Apache-common's WordUtils.wrap().
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With