String is dangerous?

According to this page, String is dangerous:

Or is this old information, because this post is made 2011?

If String can't be used, what is the safe way to simply combine two strings?

"first text"

" second text"

"first text second text"

strcat is the standard function for concatenating strings. sprintf can be useful too if you want to put strings together with other types like int or char.

There is absolutely nothing that can be done with the String class that can't be done with regular c style strings.

The strcat function can be also used with char arrays.

char message[20],one="hi",two="there";

strcat(message,one);
strcat(message,two);

Now if you read the value of char array message, it is: "hi there".

Char arrays seem pretty similar to strings, but they're different kind of variables.

Null terminated char arrays and c style string are exactly the same thing.

Thanks.

How i do this safely in one row of code:

myText = "My Sensor Value: " & digitalRead(sensorpin)

and also this in one row of code:

myFile.println("My Sensor Value: " & digitalRead(sensorpin));

How i do this safely in one row of code:

myText = "My Sensor Value: " & digitalRead(sensorpin)

I assume that you mean + and not &

Why bother?

Serial.print("fitrst ");
Serial.print("second");

Has just the same result as

String s;
s="first " + "second";
Serial.print(s);

And it does not matter that it's serial I used as an example. The same is true when sending over e-net or WiFi or I2C or SPI or ..........

Mark

summerCoder22:
Five things I never use in Arduino projects | David Crocker's Solutions blog

Or is this old information, because this post is made 2011?

The first few years while the "String" class was distributed with the Arduino IDE, this class could cause severe bugs because the class used a buggy function from the AVR LIBC library.

That bug was fixed in Arduino 1.0.5. So theoretically the "String" class now can be used with Arduino 1.0.5 and later versions without the risk of totally unexpected hanging programs without any reason.

But also now I'd never use the "String" class in any Arduino program.

Using "String" tends to

  • use more RAM than necessary
  • prevents using PROGMEM flash memory with string constants (literal strings)
  • is incompatible to all library functions
  • is slow
  • provides just a few possibly useful functions

In general I'd say that the "String" class provides just one useful function for beginners: You can concatenate String+String with using the '+' sign. For that single advantage you get for free:

  • RAM consuming programs by default
  • programs which hardly allow RAM saving after the code is established
  • incompatibility to very common library functions
  • slow program execution
  • hardly any possibilities in string handling

The native strings in C are nullterminated strings or C-strings.
They are supported by all functions of the AVR LIBC library.

holmes4:
I assume that you mean + and not &

Why bother?

Serial.print("fitrst ");

Serial.print("second");

Serial.print("fitrst "
"second");

Is even shorter and simpler

Er, arn't you missing a comma AWOL? or are my eyes going?

Mark

No, I'm not missing anything

According to this page, String is dangerous:

Yes, it will give you brain cancer just like cell phones do!

How i do this safely in one row of code:

Why do you think you need to? You don’t.

If you want to program embedded systems, they you will need to get used to managing memory and using the low-level functions.

Using C++ Strings is like learning to cook out of boxes. Never a real good meal.

Honestly you can use String as long as you never push the RAM limits. Lots of things don't.

You get used to the String and if/when you do want more it probably won't occur to you that you have a choice since that would mean learning what String is supposed to be there to save you from, having to think about details at who cares what cost?