Hi I am here for a school project i am working on some strings I can't get strings to be reasignned
String string1 = "Ace";
Serial.print(string1);
string1 = "Queen";
Serial.print(string1);
This just keeps giving me ace and not queen thx for the help
I know char exist but people keep telling me it is meant for a single charchetr is there anyway without using replace as that ruins my code
Posting a snippet is not useful.
Posting a complete sketch that illustrates the problem in question, is.
Or, in this case, posting a complete sketch that illustrates that there is no problem, is.
Sketch:
void setup() {
Serial.begin(115200);
String string1 = "Ace";
Serial.print(string1);
string1 = "Queen";
Serial.print(string1);
}
void loop() {
}
Output:
AceQueen
QED
Those people are confused, so you may be asking in the wrong places.
See C Library – <string.h> | GeeksforGeeks
void setup() {
Serial.begin(115200);
while(!Serial) delay(1); //wait for connection
char string[30]; //room for 29 characters plus terminating zero
strcpy(string,"Ace");
Serial.println(string);
strcpy(string,"Queen");
Serial.println(string);
}
void loop() {
}
Prints
Ace
Queen
28ri0203:
that fixed it
What was wrong with your original sketch ?
1 Like
J-M-L
April 24, 2025, 6:17am
6
Something else OP did not share
The snippet from the first post is correct so I suspect an issue with scope and redefinition of the variable in another function or compound statement and then loosing that scope and printing again the original variable
Something like
String string1 = "Ace";
Serial.print(string1);
if (someCondition) {
String string1 = "Queen";
} else {
String string1 = "King";
}
Serial.print(string1);
To @28ri0203 - if that is close to what you had or never heard about the word scope, it’s time to read about scope
1 Like
system
Closed
October 21, 2025, 6:18am
7
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.