Electronic – arduino – Since Arduino 1.0 string to upper case conversion does not work

arduinodebugging

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!

Best Answer

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();