Tokenizing a String

I have the following code where i am getting the data in the following from: “3,0,null,Simple”

I want to tokenize this, and break the string and return it back into an array so that
s[0] = “3”
s[1] = “0”
s[2] = “null”
s[3] = “Simple”

I wrote the following code and was wondering if this was correct, and if there was a better way to do this?



String* TokenizeString(String Input){
    
    int firstCommaIndex = Input.indexOf(',');
    int secondCommaIndex = Input.indexOf(',', firstCommaIndex+1);
    int thirdCommaIndex = Input.index(',', secondCommaIndex+1);
    
    String sNumber = Input.substring(0, firstCommaIndex);
    String sTimer = Input.substring(firstCommaIndex+1, secondCommaIndex);
    String sType = Input.substring(secondCommaIndex+1);
    String sIntent = Input.substring(thirdCommaIndex+1);
    
    String sReturnString[4] = {sNumber, sTimer, sType,  sIntent};
    
    return sReturnString;
      
}

I would stay away from String but use vanilla C strings.
And with that you could use strtok() or something like this

struct myData {
  int number;
  int timer;
  char s[2][16];
};

// Input:
//   stringToParse like "3,0,null,Simple"
//   *outData address of parsed data
// Returns:
//    number of successfully parsed fields in outData 
int TokenizeString(const char* stringToParse, myData *outData) { 
  return sscanf(stringToParse
               , "%d,%d,%15s,%15s"
               , &outData->number
               , &outData->timer
               , outData->s[0]
               , outData->s[1]);
}

(see sscanf())

2 Likes

I agree with ScruffR, strtok is a better option in my opinion as well.

However if you stick with String, this won’t work:

    String sReturnString[4] = {sNumber, sTimer, sType,  sIntent};    
    return sReturnString;

You can return a String object from a function, because the String object will be copied, even if the source is a stack allocated variable.

You cannot return an array of anything (including String), because the C compiler can’t copy an array by value. That code is not a good idea because the array will be allocated on the stack and disappears when the function exits, before it can be used. It might happen to work sometimes, but it’s a really bad idea.

1 Like