Some years back I was warned of using the Arduino String class by a very experienced developer so I've avoided it ever since. Of course when a non-devloper like me uses char without due care all sorts of weird behavior can arise, usually due to memory overwrites caused by poor pointer control.
Now I'm reading non-arduino info on the std::string class and wondering why Arduino bothered to add the String class? And how to use the std::string class in Arduino.
As a primer, I asked Google's AI engine Bard : see below.
Did Bard get it right?
Q: In Arduino what is the String type, when compared to std::string in C++?
Response:
Both Arduino's String type and C++'s std::string are used for representing text data, but they have some key differences:
Memory Management:
Arduino's String: It manages its own memory dynamically, which means that the Arduino runtime will automatically allocate and deallocate memory as needed. This makes it easier to use, but it can also lead to memory fragmentation issues if not managed carefully.
C++'s std::string: It relies on the programmer to manage memory explicitly. This gives more control over memory usage, but it also requires more careful programming to avoid memory leaks.
Performance:
Arduino's String: It is generally less performant than C++'s std::string due to its dynamic memory management. This can be noticeable in performance-critical applications.
C++'s std::string: It is typically more performant than Arduino's String due to its explicit memory management. This makes it a better choice for applications where performance is a major concern.
String Manipulation:
Arduino's String: It provides a set of methods for manipulating strings, such as length(), substring(), and concat().
C++'s std::string: It also provides a rich set of methods for string manipulation, which are generally more powerful and flexible than those of Arduino's String.
Overall:
Arduino's String: It is easier to use and does not require explicit memory management, making it a good choice for beginners and for applications where performance is not a critical concern.
C++'s std::string: It offers more control over memory usage and is typically more performant, making it a better choice for advanced programmers and for performance-critical applications.