String getting reinitialized to "" every time a while loop runs.

Hello,
I am running this code in the setup portion of my program.

String response2;
while (response2.indexOf("Date") <= 0)
{
   esp.write("\r\n");
   response2 = esp.readString();
   Serial.println(response2);
}

I am not sure why, but the variable response2 is getting reinitialized to "" every single time the loop runs, and as a result, never breaks out of it.

Any help would be much appreciated.

Thanks.[tt][/tt]

response2 is getting reinitialized to "" every single time the loop runs

What loop? If you mean the "while" loop, why do you think that the ESP has a string ready for you to read, when you try to read it?

Not a good idea to use Strings, as they cause memory problems on Arduino. Use C-strings (character arrays) instead.

I wonder what the rest of the subroutine looks like. If this is in loop(), then response2 is a local variable and will be reinitialized each time that main() calls loop(). But this is only speculation as the complete subroutine is not available for review.

adwsystems:
I wonder what the rest of the subroutine looks like. If this is in loop(), then response2 is a local variable and will be reinitialized each time that main() calls loop(). But this is only speculation as the complete subroutine is not available for review.

No, I have placed all of this code in the void setup area.

jremington:
What loop? If you mean the "while" loop, why do you think that the ESP has a string ready for you to read, when you try to read it?

Not a good idea to use Strings, as they cause memory problems on Arduino. Use C-strings (character arrays) instead.

Yes, I do believe that the esp has a string ready for me because after some time in the while loop I am getting a valid response printed to the Serial monitor, but even after printing this response which has the String "Date" in it, the Arduino does not break out of the loop.

jremington:
What loop? If you mean the "while" loop, why do you think that the ESP has a string ready for you to read, when you try to read it?

Not a good idea to use Strings, as they cause memory problems on Arduino. Use C-strings (character arrays) instead.

Tried your solution... I pasted a copy of the response I should be getting as the value of response2 and when I compiled it, I got a message saying that I was using 87% of dynamic memory. as I am reading various other strings from the ESP in my program, I think this is the issue.

Thank you for your help.