Okay, this may work for you. It breaks the line as normal using just spaces, and you can put in a | to force a break, which can create paragraphs.
Check out the example. The switch on the very right will be of interest to you.
Code: Select all
int lineCount = 0;
function str blockFormatString(str inputString, int charsPerLine) {
str Result = "";
//reset this to 0 so it can be reused on new calls
lineCount = 0;
//Loop through and determine how to divvy out the lines.
while(strlen(inputString) > charsPerLine) {
//Check to see if the string has the escape character: |
//This is wrapped in an if else block due to possibility of breaking if the string to modify is around
//charsPerLine and the leftovers from this function get run through the next if/else block. For example,
//if we had "The Quick Brown fox jumps over the lazy dog. |The quick", at the end of a string, with a
//charsPerLine of say 30, "The quick" would be leftover and would not be able to run through the if or else
//blocks because both use getChar, which would return an error, because there isn't a 30th position in that string.
//So thus we restart the while loop to recheck the condition.
//Create a copy of the theoretical line that we can check through and modify.
str tempStr = stringSubstring(inputString, 0, charsPerLine);
if(stringContains(tempStr,"|")) {
//keep track of the last | position so we know what to chop off of inputString.
int lastPosition = stringFindLast(tempStr, "|") + 1; //note index starts at 0
//using this because stringRemove has a glitch where it will not remove the last character.
tempStr = stringSubstring(tempStr, 0, lastPosition);
//Loop through the string
for(int index = 0; index < strlen(tempStr); index++) {
//check the string for '|' and add to the linecount for each one.
if(getChar(tempstr, index) == '|') {
lineCount++;
}
}
tempstr = stringReplace(tempstr, "|", "\n", 0, 0);
//Add this to the result, remove the stuff we just added
result = concatenateStrings(2, result, tempstr, "", "");
inputString = stringRemove(inputString, 0, lastPosition);
} else {
//check the char after the end of the line to check if it's a space
if(getChar(inputString, charsPerLine) == ' ') {
//If so, concatenate the proper text into the result.
result = concatenateStrings(3, result, stringSubstring(inputString, 0, charsPerLine + 1), "\n", "");
//Clean out the stuff we just concatenated into the result
inputString = StringRemove(inputString, 0, charsPerLine + 1);
lineCount++;
//Else, FIND A SPACE TO BREAK AT!
} else {
bool found = false;
//Cycle backwards through the string until a space is found (or run out of characters).
//Initialze our index here at charsPerLine - 1 because the index in Strings starts at 0.
for(int i = charsPerLine - 1; i > 0 && !found; i--) {
//Check for a space at the current character
if(getChar(inputString, i) == ' ') {
found = true;
//the i + 1 is to copy the found space.
result = concatenateStrings(4, result, stringSubstring(inputString, 0, i + 1), "", "\n");
inputString = stringRemove(inputString, 0, i + 1);
lineCount++;
}
}
//If no space was found, break the line off anyway.
if(!found) {
result = concatenateStrings(3, result, stringSubstring(inputString, 0, charsPerLine), "\n", "");
inputString = stringRemove(inputString, 0, charsPerLine);
lineCount++;
}
}
}
}
//And this takes care of a hanging line.
if(strlen(inputString) > 0) {
//loop through the remainder of the string and check for |
for(int index2 = 0; index2 < strlen(inputString); index2++) {
//check the string for '|' and add to the linecount for each one.
if(getChar(inputString, index2) == '|') {
lineCount++;
}
}
//replace the | with \n
inputString = stringReplace(inputString, "|", "\n", 0, 0);
result = concatenateStrings(2, result, inputString, "", "");
lineCount++;
}
return result;
}