Hi everybody,
because of my limited coding experience I hope to get some feedback and help.
For my project I need to process text messages with an maximum of 200 characters. To display the text on dot matrix displays, I need to break the message into lines (in my example 5 lines, max. 20 character per line) without splitting the words. I tried something with strtok() which works so far. But I'm sure that my script is not very eloquent and consumes a lot of sRAM. And there is still a problem to solve: there should be an exception that very long words have to get split anyway.
Please excuse my "dirty code", I started with Arduino and scripting just a couple of months ago...
Here is what I did, working so far:
String lineOne;
String lineTwo;
String lineThree;
String lineFour;
String lineFive;
char str[] ="Hello World! Here is some text. I need them to split into multiple lines without breaking the words.";
char * pch;
void setup() {
Serial.begin(9600);
Serial.println("Start splitting");
pch = strtok (str," ");
// First Line
while (pch != NULL)
{
String stringPch = pch;
if (lineOne.length() + stringPch.length() < 21)
{
lineOne = lineOne + pch + " ";
pch = strtok (NULL, " ");
}
else
{
break;
}
}
// Second Line
while (pch != NULL)
{
String stringPch = pch;
if (lineTwo.length() + stringPch.length() < 21)
{
lineTwo = lineTwo + pch + " ";
pch = strtok (NULL, " ");
}
else
{
break;
}
}
// Third Line
while (pch != NULL)
{
String stringPch = pch;
if (lineThree.length() + stringPch.length() < 21)
{
lineThree = lineThree + pch + " ";
pch = strtok (NULL, " ");
}
else
{
break;
}
}
// Fourth Line
while (pch != NULL)
{
String stringPch = pch;
if (lineFour.length() + stringPch.length() < 21)
{
lineFour = lineFour + pch + " ";
pch = strtok (NULL, " ");
}
else
{
break;
}
}
// Fith Line
while (pch != NULL)
{
String stringPch = pch;
if (lineFive.length() + stringPch.length() < 21)
{
lineFive = lineFive + pch + " ";
pch = strtok (NULL, " ");
}
else
{
break;
}
}
Serial.println(lineOne);
Serial.println(lineTwo);
Serial.println(lineThree);
Serial.println(lineFour);
Serial.println(lineFive);
}
void loop() {
}