Split a String by Delimiters

Download vector.h from https://www.arduinolibraries.info/libraries/vector

 

Function

this function will take a string that is using commas to separate the values (AKA CSV format) and return a vector that is populated with the values.

the value of the following code is binary

splitStringToVector("lonely,binary")[1] 

 

std::vector<String> splitStringToVector(String msg){
  std::vector<String> subStrings;
  int j=0;
  for(int i =0; i < msg.length(); i++){
    if(msg.charAt(i) == ','){
      subStrings.push_back(msg.substring(j,i));
      j = i+1;
    }
  }
  subStrings.push_back(msg.substring(j,msg.length())); //to grab the last value of the string
  return subStrings;
}

 

 

 

 

RELATED ARTICLES

Leave a comment

Your email address will not be published. Required fields are marked *

Please note, comments must be approved before they are published