String comparison - Android

how to compare two string values in android
how to compare two strings in android studio
android string indexof
android string split
android string trim
java string compare
comparison of two strings
java string not equal

I'm unable to compare two strings using the following code:

I have a string named "gender" which will have "Male" or "Female" as its value.

if(gender == "Male")
   salutation ="Mr.";
if(gender == "Female")
   salutation ="Ms.";

This didn't work, so I tried the following:

String g1="Male";
String g2="Female";
   salutation ="Mr.";
   salutation ="Ms.";

Again, it didn't work. Can someone please tell me how to compare string values using the if statement.

Try this

 salutation ="Mr.";
 salutation ="Ms.";

Also remove ;(semi-colon ) in your if statement


In Java, one of the most common mistakes newcomers meet is using == to compare Strings. You have to remember, == compares the object references, not the content.

if statement comparing strings in java android, In java, when using == on two objects, you're not actually comparing the strings themselves. You'll need to use .equals(String) . == actually  To run the app from android studio, open one of your project's activity files and click Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen − In the above result, it is showing the result of string compare techniques. Click here to download the project code

String unlike int or other numeric variables are compared in Java differently than other languages.

To compare Strings in Java (android) it is used the method .compareTo();

so the code should be like this:

   salutation ="Mr.";
   salutation ="Ms.";

How to compare String equality in Java, At the end of this tutorial I'll also discuss why the == operator doesn't work when comparing Java strings. Solution 1: Java String comparison with  String unlike int or other numeric variables are compared in Java differently than other languages. To compare Strings in Java (android) it is used the method .compareTo();

In Java we don't compare string as you are doing above... Here is String comparison...

    if (gender.equalsIgnoreCase("Male")) {
        salutation = "Mr.";
    } else if (gender.equalsIgnoreCase("Female")) {
        salutation = "Ms.";

String, substring(1, 2);. The class String includes methods for examining individual characters of the sequence, for comparing strings, for  In this tutorial we are comparing two different string values who have different values inside it. We are using equals() method which is case sensitive and return true or false. So here is the complete step by step tutorial for Compare two Strings in Java Android program with case sensitive example.

I think the above mentioned answer is correct.Because == is for testing whether two strings are the same object,whereas .equals() tests whether two strings have the same value.

Compare two Strings in Java, Android: How to Upload an image on Firebase storage? How to validate a Password using Regular Expressions in Java. so if you have 2 strings that you want to compare, use the equals() method on String. for eg boolean resultOfComparison=stringA.equals(stringB); share | improve this answer

The == operator checks to see if two objects are exactly the same object. Two strings may be different objects, but have the same value (have exactly the same characters in them). Use the .equals() method to compare strings for equality.

How to Compare two String in Java - String , String comparison is a common programming task and Java provides several way to compare two String in Java. String is a special class in  In java, when using == on two objects, you're not actually comparing the strings themselves. You'll need to use .equals(String). == actually compares the two object's references, not their values. string1.equals(String target) compares the two strings based off of the actual characters in the strings.

String Comparison in Kotlin, 2. Comparison Operators. Let's start with the “==” operator. This operator can be used to check if the strings are structurally equal. It's the  1) String compare by equals() method. The String equals() method compares the original content of the string. It compares values of string for equality. String class provides two methods: public boolean equals(Object another) compares this string to the specified object. public boolean equalsIgnoreCase(String another) compares this String to another string, ignoring case.

Comparing Two Strings In Java | String Comparison In Java, In this article we will focus on comparing two strings in Java with suitable examples and see how it serves different purposes for string  Compare Strings in Kotlin using== operator, or compareTo(other: String,ignoreCase: Boolean=false) extension function. Kotlin Example programs are provided for a better understanding.

Equality in Kotlin ('==', '===' and 'equals'), We often need to compare the data of two variables or objects or the references of two objects in Kotlin. Kotlin Android But in the case of string comparison, if only checks the contents of the string which were equal so it returns true for  You can use either getString(int) or getText(int) to retrieve a string. getText(int) retains any rich text styling applied to the string. String array. An array of strings that can be referenced from the application.

  • Have you get solution from answers ?
  • possible duplicate of How do I compare strings in Java?
  • Possible duplicate of How do I compare strings in Java?
  • If you explain why, I'll +1 this :)
  • Look at your code carefully and remove the ; at the end of both the if statements.
  • Sorry, i wasn't using ; after the if stmt in my actual code, error while typing the question here!
  • @RaghavKumar What's the input?
  • For null-safety, I always put the constant on the left side: if("Male".equals(gender))
  • This is a better explanation why not to use ==
  • there is no "above" answer, for the order varies with number of votes. please edit and specify, if possible. tks