To answer your question, String objects are very powerful, and they can do exactly what you ask. If you restrict your parsing rules directly from the input, your code becomes less flexible, less reusable, and slightly confusing.
Strings have an indexOf () method that allows you to search for an index in a String character array of a specific character. If the character is not found, the method should return -1. The second parameter can be added to the function call to indicate the starting point for the search. In your case, since your delimiters are commas, you should call:
int commaIndex = myString.indexOf(',');
You can then use this index to create a substring using the substring () method of the String class. This returns a new String, starting at a specific starting index, and ending immediately before the second index (or the end of the file if none are specified). So you would print something similar to:
String firstValue = myString.substring(0, commaIndex); String secondValue = myString.substring(commaIndex + 1, secondCommaIndex); String thirdValue = myString.substring(secondCommaIndex + 1);
Finally, integer values ββcan be obtained using the undocumented method of the String class, toInt ():
int r = firstValue.toInt(); int g = secondValue.toInt(); int b = thirdValue.toInt();
More information about the String object and its various methods can be found in the Arduino documentation .
dsnettleton
source share