Print a String named from the value of another String

Hi!

I'm sorry about the bad subject, but I don't know how to name this topic better.

I'm trying to create a String, name it from a substring from another String, give it the value from a substring from the other String and be able to print the created String to the serial monitor at a later point. Creating the String is not a problem. The problem is that I don't know how to print the String later, because the String it was created from will change.

Here is the code:

String symbol = "1234TestString";

void setup() {
  Serial.begin(115200);
}

void loop() {

  String (symbol.substring(4, 14)) = symbol.substring(0, 4); //Creates a String called TestString which contains the value "1234"
  Serial.println(symbol.substring(4, 14) + symbol.substring(0, 4)); //Prints "TestString1234" to the serial monitor. Just for proof of concept
  delay(1000);

}

So, the String created in the loop will be called TestString and have the value "1234". I can't simply do

String symbol = "1234TestString";

void setup() {
  Serial.begin(115200);
}

void loop() {

  String (symbol.substring(4, 14)) = symbol.substring(0, 4); //Creates a String called TestString which contains the value "1234"
  Serial.println(symbol.substring(4, 14) + symbol.substring(0, 4)); //Prints "TestString1234" to the serial monitor. Just for proof of concept
  Serial.print(TestString);
  delay(1000);

}

because the sketch won't compile since TestString doesn't exist yet.

Any ideas are greatly appreciated!

Regards,
Christian

you can't create variable names at run time... variable names disappear after the compilation, you are left just with pointers to memory

what you want to do is allocate storage in memory that will hold the information you need.

I was afraid that would be the answer.
Oh well, there are many other ways of getting the result I'm after.

Thank you for the swift reply!

chrstrvs:
I was afraid that would be the answer.
Oh well, there are many other ways of getting the result I'm after.

Thank you for the swift reply!

nothing to be afraid of :slight_smile: it's actually a good thing since your arduino does not have much memory anyway..