How to concat a string with itself

Hello,
I get an error for this:

String str="A";
str+="B";

Same problem like that:

String str1="A";
String str2="B";
String str;

str=str1+str2;

What happens when you try the obvious: str = str + str;?

Don't use the "String" class on an Arduino - it will cause memory corruption and eventually a crash.

...and don't put code outside a function.

And post code, not pictures of code.

Wally, do you know about C strings?

String is a C++ thing made for computers with lots of RAM and system features like DMA. They make some things easier and others a real pain, they do things that are BAD for small RAM machines like Arduino and waste cycles doing them.

C strings are char arrays with ASCII text codes in the array members. The text ends with a char == 0. The text must be shorter than the array enough to have that ending 0 but you can put 'A' 'B' 'C' 0 into the 1st 4 chars or a 40 char array just fine and have space for 36 more text characters.

C string prints the same as C++ String.
C string array chars can be worked on directly in the array, it's easier than with C++ String.
C string array is simple and direct, unlike C++ String.

#include <string.h> // standard C string library

char str[12] = "ABC"; // str is now "ABC" with terminating 0

.....

void funct() // a function
{
....
strcat( str, str ); // str is now "ABCABC" with terminating 0
....
}

GoForSmoke:
strcat( str, str ); // str is now "ABCABC" with terminating 0

I wouldn't do that. The behavior is undefined when strcat is called with overlapping strings (calling strcat with the same string twice is a definite case of "overlapping" strings).

GoForSmoke:
String is a C++ thing made for computers with lots of RAM and system features like DMA.

I'm struggling to see what DMA has to do with the argument for or against Strings.

TheMemberFormerlyKnownAsAWOL:
I'm struggling to see what DMA has to do with the argument for or against Strings.

String makes a copy to add a char, doesn't the CPU use DMA to do that?

christop:
I wouldn't do that. The behavior is undefined when strcat is called with overlapping strings (calling strcat with the same string twice is a definite case of "overlapping" strings).

Quite right, karma to you. I had to use a buffer to make it work.

#include <string.h>

char str[4] = "ABC";
char buf[12];

void setup() 
{
  Serial.begin( 115200 );
  Serial.flush();
  strcat( buf, str );
  strcat( buf, str );
  Serial.println( "\ntest\n" );
  Serial.println( buf );
}

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

}

GoForSmoke:
String makes a copy to add a char, doesn't the CPU use DMA to do that?

Not for the sort of size of string we're talking about - the setup overhead would negate any speed advantage.

The problem with String use on the AVR is down to heap management.

GoForSmoke:
String makes a copy to add a char, doesn't the CPU use DMA to do that?

If DMA stands for 'Direct Memory Access', then DMA strategy/controller is by-passing the CPU and making direct data transfer between peripheral and RAM.

Self-copying eats cycles.

You can use String reserve command to make buffer space in a String just like with C string arrays....
and the String will be bigger due to overhead bytes.

If DMA stands for 'Direct Memory Access', then DMA strategy/controller is by-passing the CPU and making direct data transfer between peripheral and RAM.

And memory-memory operations as well, prof netgloss.