Problems trying to concatenate Strings

Hi there!
I have problems with Strings

  1. String voltage="Temperatura del sensor: " +"jjjjjj";

  2. Serial.println("Temperatura del sensor: " + "jjjjjj" );

In the first case we have 2 Strings concatenated with + simbol. This report error.

I Think that this is a basic thing in programing stuff in C, JAVA or other languaje. Arduino came from C so I don't understand the error.

If I don't concatenate no error. If I concatenate with a variable no error However with 2 Strings report a error.

In the second case we have the same but in println function.

I want to do this: where temperatura and voltaje are int variables

Serial.println("Temperatura del sensor: " +temperatura + "voltaje: " +voltaje );

thanks

Muchas gracias

You should not be trying to concatenate String objects on an Arduino. It's a sure-fire way of mullering your memory.

Instead, just print each section of your string literal separately:

Serial.print("Temperatura del sensor: ");
Serial.print(temperatura);
Serial.print(" voltaje: ");
Serial.println(voltaje);

If you must build up a single string with embedded variable content, you should really be using char arrays (C strings) and using sprintf() to construct the content from a format string:

char buffer[50];
sprintf(buffer, "Temperatura del sensor: %d voltaje: %d", temperatura, voltaje);

If the variables are floats then you would need to pre-convert them into mini character arrays first using dtostrf() and use %s instead of %d.

The operations below might be of interest.

In the first case we have 2 Strings concatenated with + simbol. This report error.

What error!

Mark

Asiergr:
In the first case we have 2 Strings concatenated with + simbol. This report error.

Incorrect, you are trying to concatenate two strings, not two Strings. The compiler interprets the string surrounded by quotes as a string, not a String. Pick Strings (Zoomkat's recommendation) or strings (everybody else's recommendation) and stick with it.

What about majenko's comment? It it wise to use 'fancy' c++ string handling on the Arduino with so little memory?

It it wise to use 'fancy' c++ string handling on the Arduino with so little memory?

It depends on how long you want your sketch to be reliable for.
If all you want is a quick demo, go ahead.

Asiergr:
I have problems with Strings

  1. String voltage="Temperatura del sensor: " +"jjjjjj";

  2. Serial.println("Temperatura del sensor: " + "jjjjjj" );

In the first case we have 2 Strings concatenated with + simbol. This report error.

This error?

sketch_jul28d:5: error: invalid operands of types 'const char [25]' and 'const char [7]' to binary 'operator+'

You can't do addition like that on the line that constructs the String object.

This compiles:

  String voltage = "Temperatura del sensor: ";
  voltage += "jjjjjj";

However as others have said, you are better off doing it other ways.

Please note that in versions of the IDE up to and including 1.0.3, the String library has bugs as discussed here and here.

In particular, the dynamic memory allocation used by the String class may fail and cause random crashes.

I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.), as described here for example.

Alternatively, install the fix described here: Fixing String Crashes

Preferably upgrade your IDE to version 1.0.4 or above at: http://arduino.cc/en/Main/Software

Hey davekw7x, I hope this reply isn't too late and gets buried, but this is my first post. I am new to Arduino and I was going through some documentation, and just playing around and practicing. I was playing with the StringAddition operator, and there is code on that page that shows how to concatenate strings. I didn't copy all of the code verbatim, just typed up some of the necessary parts the program needed, and make a bare bones version. I attached an image of what my program has and what the serial monitor reads after the code is executed. I tried messing around with different uses of print and println, removing the new lines, trying different strings and the first line is always never correctly printed to the serial monitor. This is the thread I found that this subject was most on topic. I apologize if I shouldn't have posted in this thread for this topic, I don't even know if this is where I should post this question! I hope to hear your reply!! :smiley:

stringCat.tiff (329 KB)

nrtyc4:
Hey davekw7x, I hope this reply isn't too late and gets buried, but this is my first post. I am new to Arduino and I was going through some documentation, and just playing around and practicing. I was playing with the StringAddition operator, and there is code on that page that shows how to concatenate strings. I didn't copy all of the code verbatim, just typed up some of the necessary parts the program needed, and make a bare bones version. I attached an image of what my program has and what the serial monitor reads after the code is executed. I tried messing around with different uses of print and println, removing the new lines, trying different strings and the first line is always never correctly printed to the serial monitor. This is the thread I found that this subject was most on topic. I apologize if I shouldn't have posted in this thread for this topic, I don't even know if this is where I should post this question! I hope to hear your reply!! :smiley:

Blimey... I haven't seen a TIFF file in active use for about 20 years!

In this forum we don't post images of code, we post actual code - copy it and paste it between [ code ] [ /code ] tags.

If you have a specific problem with a piece if code it is usually best to start a new forum topic with your code in it, along with what you expect to get out of it, and what you actually get out of it, rather than adding on at the end of someone else's topic (albeit a similar topic).