I have this method:
void getWeatherData() { String resp = ""; Process p; p.begin("python2.7"); p.addParameter("/root/weather.py"); p.run(); while (p.available() > 0) { char c = p.read(); resp = resp + c; } Serial.print(resp); Serial.flush();}In the serial monitor, I see the string70.7|45|1030.29|06:05|20:04|04 May 2015 22:24. The delimiter here is the pipe. Now, the parts I take a send them to methods that puts the values on the TFT screen.
However, I fail to understand how to split this long string into individual parts.
Can somebody help?
Thanks
EC
- Would you like to share the platform in use?Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2015-05-05 02:31:31 +00:00CommentedMay 5, 2015 at 2:31
- Do you mean which Arduino? If so, it is the Yun. I've also added a tag for it.EC In Va– EC In Va2015-05-05 02:45:48 +00:00CommentedMay 5, 2015 at 2:45
1 Answer1
I used to have my own String class with a member method called Chomp which did exactly what you are requiring. The body was something similar to this untested code:
String chomp(String& pSrc, char pMatch){ int matchPos = pSrc.indexOf(pMatch); // Find the first occurrence of the separator if (matchPos < 0) // Separator was not found. { String ret = pSrc; // Return the remainder of the source string. pSrc = ""; // Clear the source string return ret; } String ret = pSrc.substring(0, matchPos - 1); // "Chomp" the start of the string up to the separator into the return value pSrc.remove(0, matchPos); // Remove everything up to and including the separator. return ret;}Hopefully the above code should be pretty understandable and fixable if there are any errors. Our version worked on a "this" pointer rather than passing in the source string, so we would have something like:
String sourceCoords = "12,47,292,85";int x1 = sourceCoords.chomp(",").toInt();int y1 = sourceCoords.chomp(",").toInt();int x2 = sourceCoords.chomp(",").toInt();int y2 = sourceCoords.chomp(",").toInt();Hope this is of some use.
Explore related questions
See similar questions with these tags.
