I try to run this example code on my Arduino, but since Arduino IDE version 1.0 it doesn't work no more. The example is mainly copied and stripped down from this example.
void setup() {
Serial.begin(9600);
}
void loop() {
// toUpperCase() changes all letters to upper case:
String stringOne = "test";
Serial.println(stringOne);
stringOne = (stringOne.toUpperCase());
Serial.println(stringOne);
// do nothing while true:
while(true);
}
Error message:
StringCaseChanges.cpp: In function ‘void loop()’: StringCaseChanges.cpp:12:39: error: no match for ‘operator=’ in ‘stringOne = stringOne.String::toUpperCase()’ /usr/share/arduino/hardware/arduino/cores/arduino/WString.h:83:11: note: candidates are: String& String::operator=(const String&) /usr/share/arduino/hardware/arduino/cores/arduino/WString.h:84:11: note: String& String::operator=(const char*)
On the help page one can see that:
As of 1.0, toUpperCase() modifies the string in place rather than returning a new one.
However I don't see the reason why the example code doesn't compile.
Can you help me?
Thank you!
//EDIT
OK, this one works: void setup() { Serial.begin(9600); }
void loop() {
// toUpperCase() changes all letters to upper case:
String stringOne = "test";
Serial.println(stringOne);
stringOne.toUpperCase();
Serial.println(stringOne);
// do nothing while true:
while(true);
}
However, the help page should be changed!
-
3\$\begingroup\$ You should put your solution in as an answer. \$\endgroup\$Majenko– Majenko2011年12月11日 14:15:37 +00:00Commented Dec 11, 2011 at 14:15
-
5\$\begingroup\$ Yes... Please roll back your edit and then place an answer to your question. We will be able to up vote the answer and you can even accept your own answer. \$\endgroup\$Kellenjb– Kellenjb2011年12月11日 16:03:13 +00:00Commented Dec 11, 2011 at 16:03
-
1\$\begingroup\$ The help page looks fine; it's the linked tutorial example that needs to be updated. \$\endgroup\$Chris Stratton– Chris Stratton2011年12月11日 17:21:02 +00:00Commented Dec 11, 2011 at 17:21
1 Answer 1
As of 1.0, toUpperCase() modifies the string in place rather than returning a new one.
However I don't see the reason why the example code doesn't compile.
The thing to note on the help page is that it says "Returns: none". Since the method invocation returns nothing, and you can't assign "nothing" to a string, you get that (somewhat unhelpful) error.
The solution is to simply omit the assignment, as the method invocation itself has done everything you hoped to do, and end up with this line of code:
stringOne.toUpperCase();