is String object a good way to build up string from multiple pieces?
Have not used the String object before with Teensy, need to make up a 32-char message from multiple data pieces and send it off to an external display. Serial append/concat seemed reasonable vs writing my own code to copy parts of other strings into a char array. Then I got bitten by this anomaly/bug https://www.arduino.cc/en/Tutorial/StringAdditionOperator wherein it is written: Caution: You should be careful about concatenating multiple variable types on the same line, as you may get unexpected results.
I got the code to work by putting every concat on a separate line:
At one point, with multiple appends on one line I had the remote display showing data which should have been completely out of scope, implying that I had an errant pointer (hopefully just reading, not writing to my String(34)). But this gave me pause and made me wonder if String is really "safe" to use or if it is like running with scissors (more so than C/C++ itself). I'm trying to not go down a rabbit hole here (too many of those already). Would I be "better" off with sprintf? Something else?
Have not used the String object before with Teensy, need to make up a 32-char message from multiple data pieces and send it off to an external display. Serial append/concat seemed reasonable vs writing my own code to copy parts of other strings into a char array. Then I got bitten by this anomaly/bug https://www.arduino.cc/en/Tutorial/StringAdditionOperator wherein it is written: Caution: You should be careful about concatenating multiple variable types on the same line, as you may get unexpected results.
I got the code to work by putting every concat on a separate line:
Code:
String_1 = bufferString1;
String_1 += " ";
String_1 += key1.value;
String_1 += " = ";
String_1 += key1.text_ptr;
Serial1.print('d');
Serial1.println(String_1);
At one point, with multiple appends on one line I had the remote display showing data which should have been completely out of scope, implying that I had an errant pointer (hopefully just reading, not writing to my String(34)). But this gave me pause and made me wonder if String is really "safe" to use or if it is like running with scissors (more so than C/C++ itself). I'm trying to not go down a rabbit hole here (too many of those already). Would I be "better" off with sprintf? Something else?
Last edited: