class StringUtilities {

    public static String addHTMLLineBreaks(String text, int length)
    {
        if (text == null || length > text.length() || length <= 0)
        {
            return text;
        }

        final String[] words = text.split(" ");
        final StringBuilder newText = new StringBuilder();

        int currentLength = 0;
        for (int i = 0; i < words.length; i++)
        {
            if (currentLength >= length)
            {
                newText.append("<p>");
                currentLength = 0;
            }
            newText.append(words[i]);
            currentLength += words[i].length();

            // Add space back in after each word (try to prevent beginning of
            // new line being a space. But if there are multiple
            // spaces, then just divide).

            // Check that we don't append a space to the end of original
            // sentence.
            if (i != words.length - 1)
            {
                newText.append(' ');
                currentLength += 1;
            }
        }

        return newText.toString();
    }

    private  StringUtilities();

}