February 28, 2021
Hot Topics:

Fix a Broken Page by Breaking the String

  • By Scott Nelson
  • Send Email »
  • More Articles »

The 1 pixel space is too small for most to discern. Obviously, you aren't going to change all of your data to be split up this way, so instead you will do some basic parsing to insert this. The method I used the first time worked as follows. First, define the capital letters (another of those items I would expect in standard libraries):

private static final String CAPS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

Also handy is an array of common characters where people will expect to see a line break, so you can add this char array:

private static final char[]
   BREAK_CHARS = new char[]
   {'\\', '-', '/', '_', '#', '~', '@', '$', '%', '^', '&',
    '*', '(', ')', '+' };

Finally, you can create a static constant for the HTML and CSS that creates the space.

private static final
   String HTML_FIX = "<span class='stringBreak_nbsp'> </span>";

The static constant is to conserve memory (obviously), though it should be noted that with a table with a large amount of data that needs to be parsed in this manner, the page response time will be slowed due to the increase of characters being returned. Finally, you take the data as a string as a method parameter and break it up with a nested loop:

 * Wrap on natural break characters in HTML
 * @param string the string to be wrapped
public static String htmlWrap(String string)
   StringBuffer stringFix = new StringBuffer();
   boolean fixChar = false;
   char lastChar = SPACE_CHAR;
   char thisChar = string.charAt(0);
   int strLength = string.length();
   for (int i = 0; i < strLength; i++)
      thisChar = string.charAt(i);
      for (int c = 0; c < BREAK_CHARS.length; c++)
         if (thisChar == BREAK_CHARS[c])
            fixChar = true;
      // break a capital letter, but if there are two capital
      // letters in a row, don't break them.
      if (!fixChar && i > 0 && CAPS.indexOf(thisChar) >= 0 &&
        lastChar != SPACE_CHAR && CAPS.indexOf(lastChar) < 0)
         stringFix.append(HTML_FIX + string.charAt(i));
      else if (fixChar) stringFix.append(HTML_FIX +
         string.charAt(i) + HTML_FIX);
      else stringFix.append(string.charAt(i));
      fixChar = false;
      lastChar = string.charAt(i);
   return stringFix.toString().trim();

This could definitely be done more elegantly by using a regular expression. In the case of both the project and this article, I didn't have the time to be elegant and went for effective instead.

When The Going Gets Tough, The Tough Fake It

The preceding code works great for single pieces of data. You can add characters to split on as your data requires. The example above was for a field where breaking on the capital letters was sufficient. In another project, we had a description field populated by research data, which tends to have long words. Even though the following method isn't perfect, it sufficed to keep the table from breaking:

 * For every word in the string, split the word that is longer
 * than maxWordLength into smaller words separated by space.
 * @param string the string to be wraped @param maxWordLength
 * the maximum length of a word
public static String wrapString(String string,
   int maxWordLength)
   if (maxWordLength <= 0 || string == null) { return string; }
   StringBuffer result = new StringBuffer();
   StringTokenizer st = new StringTokenizer(string, " ");
   String emptyString = "<span class='stringBreak_nbsp'> </span>";

   while (st.hasMoreTokens())
      String word = st.nextToken();
      if (word.length() > maxWordLength)
      {// if word length is too long, split the word into
       // smaller words
         int loop = word.length() / maxWordLength;
         for (int i = 0; i <= loop; i++)
            if (i * maxWordLength + maxWordLength <= word.length())
               result.append(word.substring(i * maxWordLength,
                  i * maxWordLength +
            else if (i * maxWordLength <= word.length())
               result.append(word.substring(i * maxWordLength,
         result.append(word).append(" ");
   return result.toString();

Page 2 of 3

This article was originally published on October 13, 2008

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date