problem with String::replace

Why does the first line below compile and not the second? The p comes from AsyncWebServerRequest::getParam which returns a struct with a String. I tried wrapping 'b' and '0' with String() but it didn't help.

      char debug = p->name().toInt();
      char idx = p->name().replace('b','0').toInt();

The error message is:

error: passing 'const String' as 'this' argument of 'void String::replace(char, char)' 
discards qualifiers [-fpermissive]

        

I do not know what the error message means but this:

void String::replace(char, char)

is a clue. The replace method does not return anything. That is what the documentation says also:

Returns

none

It looks like you need to do this in multiple steps:

String temp = p->name() ;
temp.replace('b','0') ;
char idx = temp.toInt();

although I am a little concerned because:
(1) The usual recommendation is to not use String on an Arduino.
(2) The toInt() method of String is documented to return a long. Storing the result in a char could be risky.

I do not use String so I may have this completely wrong. Good Luck!

Why does the first line below compile and not the second?

Because toInt() does not modify the String instance, and replace() does.

It appears as though p->name() returns a const String (or whatever it does return gets converted to a const String). You can't modify a const String.