Arduino String class

I'm exploring the Arduino String class and I've written the following two cases. I want to know which case is efficient and not causing memory fragmentation at all?
Thanks in Advance!
The variables l, m, n, o are constant for the following two cases. However, for my project these variables won't remain constant, they will change as per the user input in the webpage and I'm a bit worried about the memory fragmentation issue.
Case 1:

float l = 7.568, m = 99.7864;
byte n[] = "ESP32 ESP8266", o[] = "Arduino Mega";
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println( pc(F("a01")) );
  Serial.println( pc(F("a02")) );
  Serial.println( pc(F("a03")) );
  Serial.println( pc(F("a04")) );
  Serial.println();
  delay(1000);
}

String pc(const String& var) {
  if (var == F("a01")) {
    return String(l, 3);
  }
  else if (var == F("a02")) {
    return String(m, 4);
  }
  else if (var == F("a03")) {
    return String((char *)n);
  }
  else if (var == F("a04")) {
    return String((char *)o);
  }
  return String();
}

Case 2:

float l = 7.568, m = 99.7864;
byte n[] = "ESP32 ESP8266", o[] = "Arduino Mega";
String str((char *)0);
void setup() {
  // put your setup code here, to run once:
 Serial.begin(115200);
 str.reserve(100);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println( pc(F("a01")) );
  Serial.println( pc(F("a02")) );
  Serial.println( pc(F("a03")) );
  Serial.println( pc(F("a04")) );
  Serial.println();
  delay(1000);
}

String pc(const String& var) {
  str.clear(); //str = "";
  if (var == F("a01")) {
    return str += String(l, 3);
  }
  else if (var == F("a02")) {
    return str +=  String(m, 4);
  }
  else if (var == F("a03")) {
    return str +=  String((char *)n);
  }
  else if (var == F("a04")) {
    return str +=  String((char *)o);
  }
  return String();
}

I think the first solution will be the best. Operation with stings use a lot of time and resources.
Check safeString library, it's a better version of String default library

why use String?
consider using c-strings and dtostrf()

float l = 7.568, m = 99.7864;
byte n[] = "ESP32 ESP8266";
byte o[] = "Arduino Mega";

void
disp (void)
{
    char s  [80];
    char s0 [10];
    char s1 [10];

    dtostrf (l, 8, 4, s0);
    dtostrf (m, 8, 4, s1);

    sprintf (s, " %s %s %s %s", s0, s1, n, o);
    Serial.println (s);
}

void
loop (void)
{
    disp ();
    delay (1000);
}

void
setup (void)
{
    Serial.begin (115200);
}
   7.5680  99.7864 ESP32 ESP8266 Arduino Mega
   7.5680  99.7864 ESP32 ESP8266 Arduino Mega
   7.5680  99.7864 ESP32 ESP8266 Arduino Mega
   7.5680  99.7864 ESP32 ESP8266 Arduino Mega
   7.5680  99.7864 ESP32 ESP8266 Arduino Mega

You should not use the "String" class at all when working on AVR based MCU's. You do not need to copy a substring if you only need the end of it:

char* is_a23(char* val)
{
  if ((val[0] == 'a') && (val[1] == '2') && (val[2] == '3') && (val[3] == 0)) return &val[1];
  return (char*)0;
}

char str1[] = "a23";
char str2[] = "a24";

char *res = is_a23(str1);
if (res) Serial.println(res); //Printed

res = is_a23(str2);
if (res) Serial.println(res); //Not printed

It should be more efficient than working with "String".

No, it is a more bloated version of the String library. Don't use either on memory starved MCUs.

2 Likes

How is PString-Arduino-lib Library?

Just another wrapper around using strings (null terminated arrays of char)

Then using any of the mentioned libraries rather than using either String class or revamping the code from the beginning (c-strings or array of char) , isn't the best choice?
And what about using Arduino's built-in String library for the robust microcontrollers like ESP8266 or ESP32?

Once you are off AVR MCUs, the String class is used everywhere, including many libraries. It is a matter of available SRAM.

I was wondering, Are ESP series microcontrollers, AVR microcontrollers? because they also follow open-source RISC Architecture.

No, AVR is an Atmel thing. It is not only the available memory that differs, AVR uses a simple bootloader to launch the code directly on the hardware, whereas ESP's are running an operating system (RTOS) which then handles code execution. AVR uses a very crude memory management compared to ESP's which is more advanced.

Ohh! thanks for the information. Unnecessarily I was worrying about, using String class for ESP32!