Problem with replace() method is a string

Good morning,

I havw the following code that is similar to the exmaple in the Arduino's reference for replacing part of a string

setip()
{
** String cadena;**
** ......**
** cadena="Version 0.1";**
** .....**
** caratula(cadena,500);**
}
loop
{
** .......**
}
void caratula(String cadena, int temps)
{
** String cadena1;**
** cadena1=cadena.replace("0.","=");**
.....
}

I want to obtain "Versio =1" but I obtain the following error in the line with cadena1=cadena.replace("0.","=");
Does anybody know what is wrong in my code?

Thanks in advance

Arduino:1.8.10 (Linux), Tarjeta:"Arduino/Genuino Uno"

/home/pepe/Dropbox/ARDUINO/CODIS/DISPLAY TM1637/test_caratula/test_caratula.ino: In function 'void caratula(String, int)':
test_caratula:230:38: error: no match for 'operator=' (operand types are 'String' and 'void')
cadena1=cadena.replace("0.","=");
^
In file included from /home/pepe/Programes/arduino-1.8.10/hardware/arduino/avr/cores/arduino/Arduino.h:231:0,
from sketch/test_caratula.ino.cpp:1:
/home/pepe/Programes/arduino-1.8.10/hardware/arduino/avr/cores/arduino/WString.h:86:11: note: candidate: String& String::operator=(const String&)
String & operator = (const String &rhs);
^~~~~~~~
/home/pepe/Programes/arduino-1.8.10/hardware/arduino/avr/cores/arduino/WString.h:86:11: note: no known conversion for argument 1 from 'void' to 'const String&'
/home/pepe/Programes/arduino-1.8.10/hardware/arduino/avr/cores/arduino/WString.h:87:11: note: candidate: String& String::operator=(const char*)
Se encontraron varias bibliotecas para "TM1637Display.h"
String & operator = (const char cstr);
Usado: /home/pepe/Arduino/libraries/TM1637
^~~~~~~~
/home/pepe/Programes/arduino-1.8.10/hardware/arduino/avr/cores/arduino/WString.h:87:11: note: no known conversion for argument 1 from 'void' to 'const char
'
/home/pepe/Programes/arduino-1.8.10/hardware/arduino/avr/cores/arduino/WString.h:88:11: note: candidate: String& String::operator=(const __FlashStringHelper*)
String & operator = (const __FlashStringHelper str);
^~~~~~~~
/home/pepe/Programes/arduino-1.8.10/hardware/arduino/avr/cores/arduino/WString.h:88:11: note: no known conversion for argument 1 from 'void' to 'const __FlashStringHelper
'
/home/pepe/Programes/arduino-1.8.10/hardware/arduino/avr/cores/arduino/WString.h:90:11: note: candidate: String& String::operator=(String&&)
String & operator = (String &&rval);
^~~~~~~~
/home/pepe/Programes/arduino-1.8.10/hardware/arduino/avr/cores/arduino/WString.h:90:11: note: no known conversion for argument 1 from 'void' to 'String&&'
/home/pepe/Programes/arduino-1.8.10/hardware/arduino/avr/cores/arduino/WString.h:91:11: note: candidate: String& String::operator=(StringSumHelper&&)
String & operator = (StringSumHelper &&rval);
^~~~~~~~
/home/pepe/Programes/arduino-1.8.10/hardware/arduino/avr/cores/arduino/WString.h:91:11: note: no known conversion for argument 1 from 'void' to 'StringSumHelper&&'
exit status 1
no match for 'operator=' (operand types are 'String' and 'void')

 cadena1=cadena.replace("0.","=");

That is not how you use the replace function. It does not return a value

You could, if required, copy the original String to a new one and do the replace() on that if you need both the original and replaced versions.

See replace() function

Better still, consider using C style strings instead of String objects to avoid possible memory fragmentation

The .replace() method does not return a String so you are trying to store a 'void' in a String. If you want to modify a copy of the String, copy it first and then modify it:

void caratula(String cadena, int temps)
{
  String cadena1 = cadena;
  cadena1.replace("0.", "=");
  //.....
}

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you post your entire code please?
What model Arduino are you using?

Thanks... Tom... :slight_smile:

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

...R

UKHeliBob:

 cadena1=cadena.replace("0.","=");

That is not how you use the replace function. It does not return a value

You could, if required, copy the original String to a new one and do the replace() on that if you need both the original and replaced versions.

See replace() function

Better still, consider using C style strings instead of String objects to avoid possible memory fragmentation

Thank you for your help. My confusion came because I read this Arduino's official reference where the code is wrong.

https://www.arduino.cc/en/Tutorial/StringReplace

johnwasser:
The .replace() method does not return a String so you are trying to store a 'void' in a String. If you want to modify a copy of the String, copy it first and then modify it:

void caratula(String cadena, int temps)

{
 String cadena1 = cadena;
 cadena1.replace("0.", "=");
 //.....
}

Thank you very much for your help and code. My confusion came because I read this official Arduino's site where the code is wrong

https://www.arduino.cc/en/Tutorial/StringReplace

My confusion came because I read this official Arduino's site where the code is wrong

That is not the only place where the advice is wrong I am afraid