Suppose I have a string like "cmd,param1,param2" . String is an Arduino String type. https://www.arduino.cc/en/Reference/String
I want to extract each of the substrings separated by commas. I have successfully written code for a specific case like this. Here is the code;
String = str_data('cmd,param1,param2'); int firstCommaIndex = str_data.indexOf(','); int secondCommaIndex = str_data.indexOf(',', firstCommaIndex+1); String cmd = str_data.substring(0, firstCommaIndex); String param1 = str_data.substring(firstCommaIndex+1, secondCommaIndex); String param2 = str_data.substring(secondCommaIndex+1);
My problem is to have a function that solves the general case. A line can be divided into any number of commas. I would like to have a function that looks like this:
String parserCommaDelimited(String input_delimited_str, int nth_param_num) {
Suppose input_delimited_str="cmd,param1,param2,param3,param4"
parserCommaDelimited(input_delimited_str, 1) returns "cmd" . parserCommaDelimited(input_delimited_str, 5) returns "param4" .
c string parsing delimiter arduino
user781486
source share