You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Andrew Perepelytsya <ap...@gmail.com> on 2004/09/10 09:18:29 UTC
commons-lang new StringUtils method submission
Hello, all.
In our projects I've come across a need to split a string into a
number of fixed-length strings. Sadly, there was no ready-to-use
method in StringUtils, but hopefully, this submission may be valuable.
The method tries to do its best to present a readable result.
Feel free to perform any amendments to conform to commons-lang guidelines.
/**
* Splits the string into a number of fixed-length
* strings. Word-wrap is applied to avoid cutting
* in the middle of the word. In this case the
* string is shortened.
*
* @param s String to split
* @param maxLineLength maximum length of each line
*
* @return an array of lines with <code>length <= maxLineLength</code>
*/
private String[] splitLinesClean(String s, int maxLineLength) {
// TODO arg checks with newer commons-lang Validate class
List lines = new ArrayList();
int index = 0;
String[] words = StringUtils.split(s);
while (index < words.length) {
StringBuffer sb = new StringBuffer(maxLineLength);
while (sb.length() < maxLineLength && index < words.length) {
sb.append(words[index]).append(' ');
index++;
}
if (sb.length() > maxLineLength) {
// cut last space and word
sb.deleteCharAt(sb.length() - 1);
sb.delete(sb.lastIndexOf(" "), sb.length());
index--;
}
lines.add(sb.toString());
}
return (String[]) lines.toArray(new String[lines.size()]);
}
Regards,
Andrew
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
Re: commons-lang new StringUtils method submission
Posted by Andrew Perepelytsya <ap...@gmail.com>.
Hello, Henri.
The WordUtils seems to provide the functionality, but... (again, this
but ;) it addresses a bit different problem. Imagine you have an input
String with newlines already inserted. Then, after running WordUtils
over it, you'll have extra newlines inserted. Then how do you know
where the actual line end is. One way would be to strip away newlines
from input string before processing, but then, if you really wanted to
keep the formatting (e.g. preformatted e-mail). This could lead to
more overloaded methods with myriads of flags.
The submission in question addressed a common need while converting a
legacy application (particularly, when long report descriptions had to
be exported to excel workbooks with more or less fixed columns to fit
on paper).
With certain assumptions WordUtils could work, but it would require
more scaffolding code. And we all know we have commons-lang to handle
all that 'extra' code ;)
Regards,
Andrew
On Fri, 10 Sep 2004 11:39:29 -0400, Henri Yandell <fl...@gmail.com> wrote:
> How about the wrap methods in WordUtils? Does this have any advantage over them?
>
> Hen
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
Re: commons-lang new StringUtils method submission
Posted by Henri Yandell <fl...@gmail.com>.
How about the wrap methods in WordUtils? Does this have any advantage over them?
Hen
On Fri, 10 Sep 2004 11:18:29 +0400, Andrew Perepelytsya
<ap...@gmail.com> wrote:
> Hello, all.
>
> In our projects I've come across a need to split a string into a
> number of fixed-length strings. Sadly, there was no ready-to-use
> method in StringUtils, but hopefully, this submission may be valuable.
> The method tries to do its best to present a readable result.
> Feel free to perform any amendments to conform to commons-lang guidelines.
>
> /**
> * Splits the string into a number of fixed-length
> * strings. Word-wrap is applied to avoid cutting
> * in the middle of the word. In this case the
> * string is shortened.
> *
> * @param s String to split
> * @param maxLineLength maximum length of each line
> *
> * @return an array of lines with <code>length <= maxLineLength</code>
> */
> private String[] splitLinesClean(String s, int maxLineLength) {
> // TODO arg checks with newer commons-lang Validate class
>
> List lines = new ArrayList();
> int index = 0;
> String[] words = StringUtils.split(s);
>
> while (index < words.length) {
> StringBuffer sb = new StringBuffer(maxLineLength);
>
> while (sb.length() < maxLineLength && index < words.length) {
> sb.append(words[index]).append(' ');
> index++;
> }
> if (sb.length() > maxLineLength) {
> // cut last space and word
> sb.deleteCharAt(sb.length() - 1);
> sb.delete(sb.lastIndexOf(" "), sb.length());
> index--;
> }
>
> lines.add(sb.toString());
> }
>
> return (String[]) lines.toArray(new String[lines.size()]);
> }
>
> Regards,
> Andrew
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org