Search code examples
c++stringesp32arduino-c++

how to convert String in arduino library to std::string in c++


I have problem converting a String to std::string, to pass it to my function as the sample of my code is

String dataString = configFile.readString();
rawData = simplifyData("try to fetch data as string from dataString");

Solution

  • Since std::string has a constructor accepting a const char* as parameter you can copy your String by using this, e.g.:

    rawData = simplifyData(std::string(dataString.c_str()));
    

    Or, since this constructor is implict, you can simplify it in your function call, such as

    rawData = simplifyData(dataString.c_str());