PA3040
October 6, 2016, 5:13am
1
Have a good day All
I have a String format like this
String myString = "\0x77\1\2\2\3\0x88";
Please advice how can separate each string from above string
Example
String a = 0x077
String b = 1
String c =
Like wise
I am using Arduino UNO
Thanks in advance
guix
October 6, 2016, 5:33am
2
'' is a special character and should not be used like you do. Use another character, such as '|'. Use char arrays instead of Strings, then you can use function strtok
Here is an example: C++ code - 29 lines - codepad
+1 guix suggestion, '' is a special character used as escape char.
I posted a code to split an array using strtok_r, check this post Split string by delimiter or check string for a value. - #7 by sarouje - Programming Questions - Arduino Forum
If you wanted to use String then below code will be helpful
String split(String data, char separator, int index)
{
int found = 0;
int strIndex[] = {0, -1};
int maxIndex = data.length()-1;
for(int i=0; i<=maxIndex && found<=index; i++){
if(data.charAt(i)==separator || i==maxIndex){
found++;
strIndex[0] = strIndex[1]+1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}
//How to call
String myString = "0x77|2|2|3|0x88";
String splited = split(myString, '|',0)
//returns 0x77