Pages: [1]   Go Down
Author Topic: Not able to get substring of string  (Read 322 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 56
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
{
   // 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?
« Last Edit: February 03, 2012, 02:07:02 am by rtadams89 » Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 480
Posts: 18720
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

No.

Code:
     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.
Logged


Offline Offline
Jr. Member
**
Karma: 0
Posts: 56
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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.
Logged

Pages: [1]   Go Up
Jump to: