1

I have a problem with the following code. I cut the String "Data1", give it to "Data2" and then I check the String. My Phone always says "ab is not ab", but I do not know why? -.-

Any ideas?

String Data1 = "abc";
String Data2 = "";

Data2 = Data1.substring(0, 2);

if(Data2 == "ab")
  {
        Toast.makeText(this, Data2 + " is ab" , Toast.LENGTH_LONG).show();
  }
else
  {
        Toast.makeText(this, Data2 + " is not ab", Toast.LENGTH_LONG).show(); 
  }

Thanks ...

TomStr
  • 51
  • 5

1 Answers1

0

In Java you compare strings using the method equals(). More info here

Change your if statement as follows

if(Data2.equals("ab"))
{
    Toast.makeText(this, Data2 + " is ab" , Toast.LENGTH_LONG).show();
}
else
{
    Toast.makeText(this, Data2 + " is not ab", Toast.LENGTH_LONG).show(); 
}
Community
  • 1
  • 1
Jhon
  • 582
  • 7
  • 28