can't concat strings

Why can you do almost anything imaginable to concat strings according to... http://wiki.animusoft.com/tutorials/arduino-tutorials/arduino-working-with-strings-tutorials/arduino-concatenation-example/ ...except...

String myString;
myString="foo" + "bar";

...??? I am missing something???

I am missing something?

Well, you're missing the distinction between string and String, and we're missing almost all of your code.

On the plus side, you seem to have a surplus of question marks.

Sorry here is all the code...

void setup() {
  // put your setup code here, to run once:
String myString;
myString="foo" + "bar";
}

void loop() {
  // put your main code here, to run repeatedly:

}

Using the String class on any of the Arduino family is often not a good idea...too much memory. Try character arrays instead:

void setup() {
   char message[20] = "foo";

  Serial.begin(9600);
  strcat(message, "bar");
  Serial.println(message);
}

void loop() {

}

There is a huge number of functions available to you for using character array; strcat() is only one of them. BTW, the String version of the same program is 3482 while the code above only generates 2012 bytes.

Sorry here is all the code...

And?

You said you were missing almost all of my code so I posted it.

econjack: Using the String class on any of the Arduino family is often not a good idea...too much memory. Try character arrays instead:

void setup() {
   char message[20] = "foo";

  Serial.begin(9600);   strcat(message, "bar");   Serial.println(message); }

void loop() {

}




There is a huge number of functions available to you for using character array; *strcat()* is only one of them. BTW, the String version of the same program is 3482 while the code above only generates 2012 bytes.

Thank you!

freakdaddy: You said you were missing almost all of my code so I posted it.

But your code doesn't do anythiing.

freakdaddy: Sorry here is all the code...

void setup() {
  // put your setup code here, to run once:
String myString;
myString="foo" + "bar";
}

void loop() {   // put your main code here, to run repeatedly:

}

char array may be better, but if you want to understand String class look at it this way

adding a string constant to a string constant is not allowed...

myString = "this string" + "that string";  //nope

you have to add your string constant to a string:

void setup()
{
  String myString;
  myString = myString + "foo";
  myString += "bar";
  Serial.print(myString);
}
void loop()
{
  
}

AWOL:

freakdaddy: You said you were missing almost all of my code so I posted it.

But your code doesn't do anythiing.

Other than to demonstrate my confusion, no it really doesn't.

Well, apart from reiterating the "don't use the String class" message, I don't think I can say much more.

BulldogLowell: char array may be better, but if you want to understand String class look at it this way

adding a string constant to a string constant is not allowed...

myString = "this string" + "that string";  //nope

you have to add your string constant to a string:

void setup()
{
  String myString;
  myString = myString + "foo";
  myString += "bar";
  Serial.print(myString);
}

Ok so the concatenation is happening before the assignment and not during it.

AWOL: Well, apart from reiterating the "don't use the String class" message, I don't think I can say much more.

That's ok... Thanks... BulldogLowell explained it pretty well.

freakdaddy:

AWOL: Well, apart from reiterating the "don't use the String class" message, I don't think I can say much more.

That's ok... Thanks... BulldogLowell explained it pretty well.

there are a lot of functions in String class you may want to explore (and then possibly completely ignore per everyone's advice :blush:)

another way of doing what you wanted:

void setup()
{
  String myString = "foo";
  String myOtherString = "bar";
  myString.concat(myOtherString);
  Serial.print(myString);
}
void loop()
{
  
}

Other than to demonstrate my confusion, no it really doesn't.

Well, we can't tell what you are confused about, how you determined that the code did not do what you want. The myString variable is assigned some value, but goes out of scope immediately, so how can you tell that it was assigned an incorrect value?

As I mentioned, I could not understand why...

String myString;
myString="foo" + "bar";

...isn't allowed. It doesn't compile. It has nothing to do with being assigned an incorrect value. BulldogLowell explained that adding a constant to a constant does not work and understandably so. I'd have to assume that compiler doesn't allow it because the concat is done prior to the assignment.

Perhaps a reference would be helpful.

http://arduino.cc/en/Reference/StringConcat

I don't think the reference helped in this case. It says "Combines, or concatenates two strings into one new String. The second string is appended to the first, and the result is placed in a new String". To me, at the time, "This is a string" and "This is a string as well" are strings. So concat("This is a string","This is a string as well"); should have worked. Now if the strings are constants and the compiler cannot take two constants, concat them in memory and place them in a string var that is a different story. The reference really doesn't explain it that deep.

freakdaddy: I don't think the reference helped in this case. It says "Combines, or concatenates two strings into one new String. The second string is appended to the first, and the result is placed in a new String". To me, at the time, "This is a string" and "This is a string as well" are strings. So concat("This is a string","This is a string as well"); should have worked. Now if the strings are constants and the compiler cannot take two constants, concat them in memory and place them in a string var that is a different story. The reference really doesn't explain it that deep.

so, the right side of the assignment operator has to be calculated before the assignment can be made, right?

so here:

myString="foo" + "bar";

what makes this:

"foo"

or this:

"bar"

a String in the eyes of the compiler?

well this would...

String fooString = "foo";
String barString = "bar";

fooString += barString;

Serial.print(fooString);

you could also...

  String myString;
  myString += "foo";
  myString += "bar";
  Serial.println(myString);

Or simply

 String myString;
  myString=(String)"foo" + "bar";

(I really don't know - I can't see the point of String in this context)