arduino c string split by delimiter

Solutions on MaxInterview for arduino c string split by delimiter by the best coders in the world

showing results for - "arduino c string split by delimiter"
Fanny
12 Jan 2020
1/* Original code on Stackoverflow
2	https://stackoverflow.com/questions/29671455/how-to-split-a-string-using-a-specific-delimiter-in-arduino
3	Example : 
4		String str = "1,2,3";
5		String part01 = getValue(str,';',0); // get 1
6 		String part02 = getValue(str,';',1); // get 2
7		String part03 = getValue(str,';',2); // get 3
8		String part03 = getValue(str,';',4); // get empty string
9        
10	Documented by issa loubani, your average programmer :P 													*/
11
12// Happy coding O.O
13String getValue(String data, char separator, int index)
14{
15  int found = 0;
16  int strIndex[] = {0, -1};
17  int maxIndex = data.length()-1;
18
19  for(int i=0; i<=maxIndex && found<=index; i++){
20    if(data.charAt(i)==separator || i==maxIndex){
21        found++;
22        strIndex[0] = strIndex[1]+1;
23        strIndex[1] = (i == maxIndex) ? i+1 : i;
24    }
25  }
26
27  return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
28}