Go Down

Topic: Just struggling with this, any pointers would be welcome (Read 524 times) previous topic - next topic

ijm51000

Hi,

I have looked at this and can't see the wood for the trees!

I am use in Arduino 1.03 and in my program I use

string.replace("T", "");

and it does delete the character, however I need to do this for a few fixed characters and therefore trying to place it in a function , the Serial.print does correctly print the characters to be replaced and is there as a debug statement.

//declared at the top of program

const char stripChars [6] PROGMEM = "{}DIT";


//Sanity call to function

String striped = stripMyStr("{A}AIATADA");

//function
String stripMyStr(String strToStrip){

         for (int i = 0; i < 5; ++i)
              {    //tried uint8_t myChar
                     myChar = pgm_read_byte (stripChars + i);             
                     Serial.print(myChar);
                     strToStrip.replace(myChar, "");
             
         }
         return strToStrip;
         }


Error message from Arduino IDE is :-

error: call of overloaded 'replace(uint8_t&, const char [1])' is ambiguous

Any help much appreciated, explanation as to where I am going wrong(plain English :-) very, very welcome.

Ian

guix

Hello,

I suggest you just forget the String class and use standard C strings :)

ijm51000

Though I do agree and as such the program does work without this function, but for once in my life I have time to solve the problem the way I wish, not just take an easy way, or put it another way, retired and being pedantic :-)

guix

There are some bugs in the String class, that's why I recommend not using it.

Anyway, your problem is that "myChar" is (most likely) declared as "char myChar", and that replace function expect a String, so you have to convert. I'm not sure, but try:
Code: [Select]

strToStrip.replace( (String) myChar, "");

Nick Gammon

Code: [Select]

sketch_mar16a.ino: In function 'String stripMyStr(String)':
sketch_mar16a:15: error: call of overloaded 'replace(char&, const char [1])' is ambiguous
/Applications/Arduino_1.0.4.app/Contents/Resources/Java/hardware/arduino/cores/arduino/WString.h:164: note: candidates are: void String::replace(char, char) <near match>
/Applications/Arduino_1.0.4.app/Contents/Resources/Java/hardware/arduino/cores/arduino/WString.h:165: note:                 void String::replace(const String&, const String&) <near match>


The error message is basically telling you what you need. This compiles:

Code: [Select]

//declared at the top of program

const char stripChars [6] PROGMEM = "{}DIT";

//Sanity call to function

String striped = stripMyStr("{A}AIATADA");

//function
String stripMyStr(String strToStrip)
  {
  for (int i = 0; i < 5; ++i)
    {
    char myChar = pgm_read_byte (stripChars + i);             
    strToStrip.replace(String (myChar), "");
    }
  return strToStrip;
  }

void setup ()
{
  Serial.begin (115200);
  Serial.print (stripMyStr ("{A}AIATADA"));
}  // end of setup

void loop () { }


Please note that in versions of the IDE up to and including 1.0.3, the String library has bugs as discussed here and here.

In particular, the dynamic memory allocation used by the String class may fail and cause random crashes.

I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.), as described here for example.

Alternatively, install the fix described here:  Fixing String Crashes

Preferably upgrade your IDE to version 1.0.4 or above at: http://arduino.cc/en/Main/Software

Go Up