Go Down

Topic: Not able to get substring of string (Read 355 times) previous topic - next topic

rtadams89

Feb 03, 2012, 08:04 am Last Edit: Feb 03, 2012, 08:07 am by rtadams89 Reason: 1
I'm working with the web client example code (http://arduino.cc/en/Tutorial/DnsWebClient). I have tweaked it slightly, so that I can (theoretically) get a specific value contained in the html the arduio "GET"s. The part I modified is as follows:

Code: [Select]
{
  // if there are incoming bytes available
 // from the server, read them and print them:
 
  if (client.available()) {
    char c = client.read();
    String response = "";
    response = response + c;
    Serial.print(response);
    String output = response.substring(10);
    Serial.print(output);
  }


The printing of "response" to the serial output works, I get the HTTP headers and the full content of the HTML page. The printint of "output" however just returns a blank line. Shouldn't it output everyhting after the first 10 character's of what "response" outputs?

Nick Gammon

No.

Code: [Select]

     String response = "";
     response = response + c;
     Serial.print(response);
     String output = response.substring(10);


The variable "response" will only ever have a single character in it, which you just put there.

You are seeing more things in the serial output because this bit of code happens to be called many times.

rtadams89

Well, you are totally correct. I've been staring at code for too long today. I've got it workign now with:

Code: [Select]

void loop()
{
   // if there are incoming bytes available
  // from the server, read them and print them:
 
   if (client.available()) {
     char c = client.read();
     response = response + c;
   }

  // if the server's disconnected, stop the client:
   if (!client.connected()) {
     Serial.println();
     Serial.println("disconnecting.");
     unsigned int subStart = response.indexOf("Request:") + 10;
     String output = response.substring(subStart, (subStart+1));
     Serial.print(output);
     client.stop();

    // do nothing forevermore:
     while(true);
   }
}


Thanks for the help.

Go Up