10 years, 6 months ago.

Is there a "standard" mbed compatible C++ String class available?

Hi,

Maybe stupid question? If so please be gentle!

Thanks, krhohio

1 Answer

10 years, 6 months ago.

Have you tried doing the standard C++ thing? Here's an example:

(I have tried the example below on a Mini-DK2 board with the SPI TFT as stdout as I don't have a regular mbed with me right now, and it works, don't know what you might have to test the cout on, but it works...)

C++ strings example

#include "mbed.h"
#include <iostream>
#include <string>

string mystr;

int main()
{
      mystr = "This is a test.";
      cout << mystr << endl;
}

The mbed compiler doesn't seem to complain but on some systems you may also need a line:

using std::string;

after the #include and before you first use a string variable type.

A list of std::string functions can be found here: http://www.cplusplus.com/reference/string/string/ c_str() is a very handy one that's easy miss, it returns a char* to the string in legacy c format so that you can use the c++ string type in c functions that take expect a char pointer. e.g.

string myName = "Andy";
printf("Hello %s\n", myName.c_str());
posted by Andy A 16 May 2014