String functions in Particle Web IDE

Hey so I’m trying to do some string manipulation in a cloud function since you can only pass in a string argument. I’m decently new to the Particle IDE so I’m probably making some dumb mistakes but I just need more eyes on it. Is there possibly a library I need to include for this to work? Am I naming the functions wrong? Anything would be helpful! Thanks!

int save(String obj){ //save object with name passed in to EEPROM
    //string obj saved in form name/fuzz/vol
    int first= obj.find('/');
    int sec= obj.find_last_of('/');
    
    //splice the string "obj" into a string an two doubles using substrings and the c++ function stod
    String name= obj.substr(0, first);
    double vol= stod(obj.substr((first+1), sec));
    double fuzz= stod(obj.substr(sec+1));
    
    //you can stop reading here, this is the end of the string manipulation, I just copied over the whole function.
    const int count= 1;
    
    if(count==1){
        
        P1.changeAll(name, vol, fuzz);
        EEPROM.put(addr1, P1);
        
    }else if(count==2){
        
        P2.changeAll(name, vol, fuzz);
        EEPROM.put(addr2, P2);
        
    }else if(count==3){
        
        P3.changeAll(name, vol, fuzz);
        EEPROM.put(addr3, P3);
        
    }else if(count==4){
        
        P4.changeAll(name, vol, fuzz);
        EEPROM.put(addr4, P4);
        
    }else if(count==5){
        
        P5.changeAll(name, vol, fuzz);
        EEPROM.put(addr5, P5);
        
    }else{
        return -1;
    }
    count++;
    
    return 1;
    
    
}

When I verify it gives me errors that none of the string functions I’m using exist and that stod hasn’t been initialized at all. I know its probably something really dumb I’m just sick of stewing over it. Thanks!

There is the documentation for the String object here

Also a word of caution: String objects use dynamic memory allocation which - over time - can lead to heap fragmentation and may require a System.reset() once the device starts to misbehave to tidy up the heap.
The common advice to avoid this is to use standard C strings (aka char array) instead.
Also some C functions that expect a C string may need a (const char*) type cast when passing in a String.

1 Like

That worked! Thanks!!

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.