0

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 string 70.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

2
  • Would you like to share the platform in use? May 5 '15 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
    May 5 '15 at 2:45
0

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.