Extracting a portion of a char[] into a String (or another char[])

Hi everyone!

I'm new! I've been programming for many years professionally, but with high level languages.

Anyway, Is there an easy way to pull out elements of a char array into a String? Or even another char array?

For example, lets say I have this array: 'A','N','T','O','N','Y','C'. I want to extract say chars 2-5 into another variable to leave me with 'T','O','N','Y' or even better "TONY".

Is there an easy way to do this? I've looked at memcopy, strcat, etc... But they seem far too complex for what I'm trying to do.

Am I missing an easy way of doing this?

Thanks, Antony...

char antony[] = "ANTONYC";
char tony[5];

void setup() {
  Serial.begin(250000);
  memcpy(tony, antony + 2, (5 - 2) + 1);
  tony[4] = 0;
  Serial.print(F("tony = '"));
  Serial.print(tony);
  Serial.println(F("'"));
}
void loop() {}
tony = 'TONY'

Thanks, I'm going to give this a go soon!

If ‘ANTONY’ is constant, there is no need to waste RAM for it,
there is only a very small modification to the code neccessary.

const char antony[] PROGMEM = "ANTONYC";
char tony[5];

void setup() {
  Serial.begin(250000);
  memcpy_P(tony, antony + 2, (5 - 2) + 1);
  tony[4] = 0;
  Serial.print(F("tony = '"));
  Serial.print(tony);
  Serial.println(F("'"));
}
void loop() {}
tony = 'TONY'

Unfortunately, it isn't constant. I just said 'ANTONY' as an example. The value actually changes every 333ms. :slight_smile: