A villain called String !!

The forum is full of posts why the String should be avoided at all costs and instead all should love the char array.

If that be the case then why is it that almost all code examples for things like web server etc use the String right through … is there no other " better " option ??

  // Prepare the response
  String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
  s += "<head>";
  s += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">";
  s += "<meta http-equiv=\"refresh\" content=\"60\" />";
  s += "<script src=\"https://code.jquery.com/jquery-2.1.3.min.js\"></script>";
  s += "<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css\">";
  s += "<style>body{font-size: 24px;} .voffset {margin-top: 30px;}</style>";
  s += "</head>";

Ardubit:
The forum is full of posts why the String should be avoided at all costs and instead all should love the char array.

If that be the case then why is it that almost all code examples for things like web server etc use the String right through ... is there no other " better " option ??

the examples you see just make it easier for the entry-level beginner to understand without adding the confusing complexity and syntax of C strings compared to ease of String manipulation (after all, there is a reason to write such a class!).

The String over-hype is mostly just parrots echoing what is good common sense in embedded programming.

Examples are great for seeing an example of how to use this function or that function. But that’s all they are. They are almost never examples of good programming practice. And all those people who use them like ready made programs to modify a little and call your own are only setting themselves back.

The example you showed is an utter waste of RAM (and has too many semicolons and "'s +=") - the string should remain in flash, where it belongs.

Without getting into the discussion about whether Strings are good or not:

  1. Many of the classes in the ESP8266 Arduino core use the String class so you are more or less stuck with it.
  2. The html in the OP could be more elegantly codes as in this example and without the misery of escaping all those double quotes :
String MAIN_page = R"=====(
<!DOCTYPE html>
<html>
   <head>
      <style>
         body {font-family: helvetica,arial,sans-serif;}
         table {border-collapse: collapse; border: 1px solid black;}
         /*table {border: 1px solid black;}*/
         td {padding: 0.25em;}
         .title { font-size: 2em; font-weight: bold;}
         .name {padding: 0.5em;}
         .heading {font-weight: bold; background: #c0c0c0; padding: 0.5em;}
         .update {color: #dd3333; font-size: 0.75em;}
         output {font-size: 0.75em; color: #696969;}
      </style>
   </head>
   <script>
      function myInit() {
      }
      
      function check() {
      }
      
   </script>
   <body onload="myInit()">
   <!--     -->
   </body>
</html>

)=====";

or if you prefer in flash:

const char MAIN_page[] PROGMEM = R"=====(
<!DOCTYPE html>
<html>
   <head>
      <style>
         body {font-family: helvetica,arial,sans-serif;}
         table {border-collapse: collapse; border: 1px solid black;}
         /*table {border: 1px solid black;}*/
         td {padding: 0.25em;}
         .title { font-size: 2em; font-weight: bold;}
         .name {padding: 0.5em;}
         .heading {font-weight: bold; background: #c0c0c0; padding: 0.5em;}
         .update {color: #dd3333; font-size: 0.75em;}
         output {font-size: 0.75em; color: #696969;}
      </style>
   </head>
   <script>
      function myInit() {
      }
      
      function check() {
      }
      
   </script>
   <body onload="myInit()">
   <!--     -->
   </body>
</html>  
)=====";

why is it that almost all code examples for things like web server etc use the String

Because anybody can make an example. Very few people can make a good example.

is there no other " better " option ??

Yes, but it takes more time to learn the other option(s). Knowing how to use the standard C string functions is much harder than looking at the String class header. IMO, spending time at the beginning to learn about C strings is much more enjoyable than spending time wondering why a sketch using String crashes at random times or only runs for a limited time.

For your code snippet, you don’t say what the string is used for, but you can compare these two options:

With the String class:

#define client Serial

void setup()
{
  Serial.begin( 9600 );
  String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
  s += "<head>";
  s += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">";
  s += "<meta http-equiv=\"refresh\" content=\"60\" />";
  s += "<script src=\"https://code.jquery.com/jquery-2.1.3.min.js\"></script>";
  s += "<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css\">";
  s += "<style>body{font-size: 24px;} .voffset {margin-top: 30px;}</style>";
  s += "</head>";
  client.print( s );
}

void loop() {}

With C strings:

#define client Serial

void setup()
{
  Serial.begin( 9600 );
  client.print(
    F(
      "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"
      "<head>"
      "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"
      "<meta http-equiv=\"refresh\" content=\"60\" />"
      "<script src=\"https://code.jquery.com/jquery-2.1.3.min.js\"></script>"
      "<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css\">"
      "<style>body{font-size: 24px;} .voffset {margin-top: 30px;}</style>"
      "</head>"
    )
  );
}

void loop() {}

The String class program uses 4194 bytes of program space and 620 bytes of RAM, plus ~400 bytes of the heap (aka “dynamic memory”, which uses additional RAM). Depending on how these Strings are used in the larger program, it probably has long-term stability concerns.

The C string program uses 2442 bytes of program space and 182 bytes of RAM (for the core). Rock solid. No RAM is needed to hold on to that HTML text.

Seems like an easy choice…

-dev:
....
....

The C string program uses 2442 bytes of program space and 182 bytes of RAM (for the core). Rock solid. No RAM is needed to hold on to that HTML text.

Seems like an easy choice...

You hit the nail on the head ! And going by the responses so far, looks like I touched a raw nerve for many.. In fact after reading through your example ( thanks for taking the time to do it ) I really wonder why the String class was used at all in this case.

Of course doing it other language may ease up things but then THAT language has to be learnt ...

6v6gt:
Without getting into the discussion about whether Strings are good or not:

  1. Many of the classes in the ESP8266 Arduino core use the String class so you are more or less stuck with it.

I've noted that too, especially in the various web server classes. Given that, could something like the following be a "safer" use of the String class?

  const char message[] PROGMEM = "Hello World";
  String *aString = new String(message);
  functionThatRequiresStringArgument(*aString);
  delete aString;

My thinking is: If only one String object is used at a time and it's deleted immediately after use, then the chance of memory fragmentation / leak is reduced.

Ardubit:
Of course doing it other language may ease up things but then THAT language has to be learnt ...

It's not another language. Just plain C which is a subset of C++ (the language of Arduino).