There is now as SafeString library available for Arduino, via the Library Manager (mine :-) )
SafeStrings are GOOD for Arduino
They are only ever placed in Static or Stack memory and so completely avoid heap fragementation and small transient object construction and unnecessary data coping, NEVER causes your sketch to reboot and has extensive debugging and error messages.
SafeStrings are designed to be uses by Beginners and includes extensive error checking and detailed error messages to help you find the errors and correct your code. SafeStrings also corrects a number of errors that are in the String code.
There is a detailed tutorial at
https://www.forward.com.au/pfod/ArduinoProgramming/SafeString/index.html
and extensive examples include in the library, including parsing/tokenizing
In this case because SafeString implement almost all of String functionallity it is easy to replace EVIL Strings with GOOD SafeStrings
#include "SafeString.h"
createSafeString(dataString,60); // allocate global 60 byte safestring, just once,
// for assembling the data to log
// You could also use createSafeString within the method if you want a local SafeString
.. .. ..
void setup() {
Serial.begin(9600);
// ....
SafeString::setOutput(Serial); // enable sending debug/error msgs to Serial
// So you will get a message if dataString runs out of space (but no reboots)
// ....
}
// Data preparation for file saving:
// Add time tag:
dataString = Time0; dataString += ","; // or ',';
// Append the MPU6050 data to the string:
// no new strings created here!!!
dataString += AcX; dataString += '",';";
dataString += AcY; dataString += '",';";
dataString += AcZ; dataString += '",';";
dataString += GyX; dataString += '",';";
dataString += GyY; dataString += '",';";
dataString += GyZ;
// etc ....
// SafeStrings are printable so can use println(dataString)
// you can also print TO a SafeString so you could use, for example
// dataString.print(AcX);
// to access all the formatting print provides