variable has value 0 can't see why

Hi I am passing a uri into the arduino

http://192.168.0.177/?lightModeStandardLights=1

I have stripped StandardLights out of the string but using parseInt It always says 0 not 1, I can't see why it doesn't = 1????

#include <SPI.h>        
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,0,177 };

EthernetServer server(80);

void setup()
{
  Serial.begin(9600);
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.println("ready");

  pinMode(13, OUTPUT);
}

void loop()
{
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        if( client.find("GET /") ) {  
          while(client.findUntil("lightMode", "\n\r")){  
            
            String type = client.readString(); 
            int val = client.parseInt(); 
            
            Serial.print(type);
            Serial.print(type.substring(0,type.indexOf('=')));
            Serial.println();
            Serial.print(val);
            }
        }
        
        client.println("HTTP/1.1 200 OK");
        client.println("Content-Type: text/html");
        client.println();

        break;
      }
    }
    // give the web browser time to receive the data
    delay(1);
    client.stop();
  }
}
          while(client.findUntil("lightMode", "\n\r")){

If you are sending "GET /?lightModeStandardLights=1", to the client, after this call, "StandardLights=1" will remain to be read.

            String type = client.readString();

What DID you read? Why do you assume that '=' will terminate the reading?

            int val = client.parseInt();

What was there for the client to read?

the variable type contains the string "StandardLights=1"

The "=" isn't to terminate anything but I am using it to separate the string.

val should = 1

the variable type contains the string "StandardLights=1"

Exactly, so the call to the parseInt() method is going to get an empty string, which, when converted to an int is zippity do dah. Which is what you are getting.

Once you know that the stream contained "lightMode", you could use findUntil() to capture/throw away everything up to the '=', and THEN parseInt() would have something to read and parse.

I thought parse int read the whole string and stopped at the first integer

I have done a simple test and did a int val = client.parseint before any string stripping and when I print I get the correct value of 1.

So I am confused why I cant parse int after stripping the string into a variable??

Or have I things mixed in my head?

Think I have I have it using substring, am I right in thinking you empty the buffer when you read it's contents??

thanks

I thought parse int read the whole string and stopped at the first integer

No. There are two problems with this statement. First, by the time you call parseInt(), there is nothing left in the stream to read. The second is that parseInt() consumes everything from the stream that IS an int. It does not read and discard non-digits until it finds stuff that could be an int.

So I am confused why I cant parse int after stripping the string into a variable??

Because you are not getting data to convert to an int from the variable. You are getting it from the input stream, which you have emptied.

There is a completely different process for getting int data from a String.

m I right in thinking you empty the buffer when you read it's contents??

Of course you do.

from the docs:

Serial.parseInt() returns the first valid (long) integer number from the serial buffer. Characters that are not integers (or the minus sign) are skipped. Serial.parseInt() is terminated by the first character that is not a digit.

the issue with this code is:

String type = client.readString();

which empties the serial buffer - if you remove this line; you'll find the number will be parsed with the parseInt function; what you should do; if you need to have the variable "type" for later is to use the stdlib c functions to extract the number instead.