Programmer's Cookbook

Recipes for the practical programmer

Thursday, February 09, 2006

 

Text Wrapping Function for Java

Note that the Jakarta commons-lang package has similar functionality. I would only use this were using commons-lang was not an option.

This function takes a string value and a line length, and returns an array of lines. Lines are cut on word boundaries, where the word boundary is a space character. Spaces are included as the last character of a word, so most lines will actually end with a space. This isn't too problematic, but will cause a word to wrap if that space pushes it past the max line length.

String text = "this is a long line of text that needs to be wrapped";
String [] lines = wrapText(text, 20);
for (int i = 0; i < lines.length; i++) {
System.out.println(lines[i]);
}


static String [] wrapText (String text, int len)
{
// return empty array for null text
if (text == null)
return new String [] {};

// return text if len is zero or less
if (len <= 0)
return new String [] {text};

// return text if less than length
if (text.length() <= len)
return new String [] {text};

char [] chars = text.toCharArray();
Vector lines = new Vector();
StringBuffer line = new StringBuffer();
StringBuffer word = new StringBuffer();

for (int i = 0; i < chars.length; i++) {
word.append(chars[i]);

if (chars[i] == ' ') {
if ((line.length() + word.length()) > len) {
lines.add(line.toString());
line.delete(0, line.length());
}

line.append(word);
word.delete(0, word.length());
}
}

// handle any extra chars in current word
if (word.length() > 0) {
if ((line.length() + word.length()) > len) {
lines.add(line.toString());
line.delete(0, line.length());
}
line.append(word);
}

// handle extra line
if (line.length() > 0) {
lines.add(line.toString());
}

String [] ret = new String[lines.size()];
int c = 0; // counter
for (Enumeration e = lines.elements(); e.hasMoreElements(); c++) {
ret[c] = (String) e.nextElement();
}

return ret;
}

Comments:
Thank you very much. It helped me save a lot of time.
 
Robert ... thanks a lot for posting this function. It helped me also to save lot of time. Keep up the good work!
 
awesome!!! your help saved my day!! thanks a lot for sharing this!!!!!!
 
Thank you ..
 
This code works if you have words which lengths are less than 'len', otherwise it will fail...
 
Thanks a lot this code helped me save a lot of time and also impress my manager :)
 
Thank you, works great!
 
Thanks, useful function! :)
 
my day is saved
 
This comment has been removed by the author.
 
my day is saved
 
What compiler are you using please, because my Bluej compiler will not let it work? Mike xhtml@live.co.uk
 
What compiler are you using please, because my Bluej compiler will not let it work? Mike xhtml@live.co.uk
 
This function was very very helpful! Thanks! I made two modifications, though:

ArrayList lines = new ArrayList(); (instead of Vector)


(Instead of using Enumarator)

String [] ret = new String[lines.size()];
for (int i = 0; i < lines.size(); i++) {
ret[i] = lines.get(i);
}
I was receiving some warnings... This way I didn't receive any warning and its working correctly!
 
This comment has been removed by a blog administrator.
 
Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?