I have this method:

void getWeatherData() {
    String resp = "";

    Process p;

    while (p.available() > 0) {
        char c = p.read();
        resp = resp + c;


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?



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

1 Answer 1


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.