/**
* 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
* @param subStrLen The maximum character length of each line
* @return ret The formatted string
*/
private String splitString(String str, int subStrLen) {
/** if the string is already smaller then the maximum length return it */
if (str.length() < subStrLen) {
return str;
}
/** offset where to split the string, has to start at 0 */
int indexNum = 0;
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] + " ";
}
/** set the new sub string offset */
indexNum = (ret.length() + subStr.length() - 1 - (3 * j));
/** add the sub string to the return string */
ret += subStr + "<p>";
/** catch the error if the string has not been able to split on white spaces */
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Error: It appears that the array is out of bounds (" + e + ")");
}
/** if the sub string does not contain white spaces return it with an ending dash */
} else {
indexNum += subStrLen;
ret += tmp + "-<p>";
}
/** 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);
}
}
}
/** return the formatted string */
return "<html>" + ret + "</html>";
}