All pastes #900692 Raw Edit

Untitled

public text v1 · immutable
·published 2008-02-12 02:51 UTC
rendered paste body
  /**
   * Add linebreaks (paragraphs) to split a long string up into smaller (subStrLen) parts
   * which are then being checked if they contain a white space so the sentence is preferably
   * being split up after white spaces.
   * 
   * @param str The string being put in that has to be split up
   */
  private String splitString(String str) {

    /** offset where to split the string, has to start at 0 */
    int indexNum = 0;
    
    /** the maximum length of each line */
    int subStrLen = 40;
    
    String tmp = "", ret = "";

    for (int i = 0,  j = 0; i < str.length(); i += subStrLen, j++) {

      /** take the part of the string that has not been split yet */
      tmp = str.substring(indexNum);

      /** check if the string still has to be split */
      if (tmp.length() > subStrLen) {

        /** split the string into the maximum sub string */
        tmp = tmp.substring(0, subStrLen);

        /** check if the sub string contains a white space */
        if (tmp.contains(" ")) {

          /** try to split the sub string into words */
          try {

            /** split the sub string into words on white spaces */
            String[] subTmp = tmp.split(" ");
            
            String subStr = "";

            /** add the complete words to the sub string */
            for (int k = 0; k < (subTmp.length - 1); k++) {
              subStr = subStr + subTmp[k] + " ";
            }

            /** add the sub string to the return string */
            ret += subStr + "<p>";

            /** set the new sub string offset */
            indexNum = (ret.length() + subStr.length() - 1 - (3 * j));

            /** if this is the last part of the string add the last part of the sub string */
            if (str.substring(indexNum).length() < subStrLen) {
              ret += str.substring(indexNum);
            }

            /** catch the error if the string has not been able to split on white spaces */
          } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("error: " + e);
          }

          /** if the sub string does not contain white spaces return it with an ending dash */
        } else {

          ret += tmp + "-<p>";
          indexNum += subStrLen;
        }
      }
    }

    /** return the formatted string */
    return ret;
  }