Key "ON" string in the terminal to turn on LED

In the following program
I try to key in "on" string to turn on LED and "OFF" to turn off LED in the terminal,
In the terminal , I key in "ON" string , Serial.print(str); will show "ON" ,
But if(str == "ON") does not become true ,WHY

I use arduion 1.05

Arduino program

String str = "";
int LED = 7;
void setup()
{
Serial.begin(9600);
pinMode(LED,OUTPUT);
}

void loop()
{
while (Serial.available() >0)
{
str +=char(Serial.read());
delay(2);
}
Serial.print(str);
if (str.length() >0)
{
if(str == "ON")
{
digitalWrite(LED,HIGH);
}
else if(str == "OFF")
{
digitalWrite(LED,LOW);
}
str = "";
}
}

Make sure you have No Line Ending in the serial monitor selected, or avoid concatinating '\n' or '\r'.

I suspect the "else if" line.
Re-write it like this:

 void loop()
     {
         while (Serial.available() >0)  
            {  
             str +=char(Serial.read());
             delay(2);
             }
         Serial.print(str);
        if (str.length() >0)
           {
             if(str == "ON")
                {
                  digitalWrite(LED,HIGH);
                  }
            }
       if (str.length() >0)
           {  
           if(str == "OFF")
              {
               digitalWrite(LED,LOW);
               }
       str = "";
       }
     }

Dear god my eyes... That formatting is.... shiver

Very simple on/off serial code.

// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial on/off test 0021"); // so I can keep track
}

void loop() {

  while (Serial.available()) {
    delay(3);  
    char c = Serial.read();
    readString += c; 
  }

  if (readString.length() >0) {
    Serial.println(readString);

    if(readString.indexOf("on") >=0)
    {
      digitalWrite(ledPin, HIGH);
    }

    if(readString.indexOf("off") >=0)
    {
      digitalWrite(ledPin, LOW);
    }

    readString="";
  } 
}

Thanks for zoomkat

your give me the correct answer

zoomkat, can you stop encourage beginners to use blocking serial read, String, delay..?

guix
what is your meaning
do you have another great solution
could you post for the beginner