Checking String context


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Checking String context

  1. #1
    Join Date
    Nov 2003
    Posts
    22

    Checking String context

    If I have an int variable called C I can type if ( C==3 ) and it will work. But when I tried the same with a String variable it didn't work.I wrote if ( C=="B" ). What should I write to check a String's context?

  2. #2
    Join Date
    Mar 2003
    Posts
    834
    I'm assuming you meant to type "content" rather than "context".

    Yes, you don't check the contents of a String using the == operator (for reasons that are rather complicated to explain at this stage of your Java learning).

    Instead, you check the content of a String using the .equals(String) method:
    Code:
      String string1 = "Hello";
      String string2 = "World";
      if (string1.equals(string2)) {
        System.out.println("Equal");
      }
      else {
        System.out.println("Not Equal"); // This will be displayed.
      }
    ArchAngel.
    O:-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center