Monday, October 12, 2015

java - How to compare strings

MainActivity.java

package com.cfsuman.me.javaexamples;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get widget reference from XML layout
        TextView tv = (TextView) findViewById(R.id.tv);

        // Initialize few Strings
        String stringRed = "Red";
        String stringRed2 = "Red";
        String stringRED = "RED";
        String stringBlue = "Blue";
        String stringNull = null;
        String stringNull2 = null;

        // Comparing two strings using String.equals() method
        tv.setText("String.equals() " + stringRed + " = " + stringRed2 +
                " : " + stringRed.equals(stringRed2));

        // Comparing two strings using String.equals() method
        tv.setText(tv.getText() + "\nString.equals() " + stringRed + " = " + stringBlue +
                " : " + stringRed.equals(stringBlue));

        // Comparing two strings by String.equals() method
        tv.setText(tv.getText() + "\nString.equals() " + stringRed + " = "
                + stringRED + " : " + stringRed.equals(stringRED));

        // Comparing a string with another null string using String.equals() method
        tv.setText(tv.getText() +"\n\nString.equals() "+stringRed +
                " = null : " + stringRed.equals(stringNull));

        // This line throw exception when comparing with null string by String.equals() method
        //tv.setText(tv.getText() +"\nnull = " + stringRed+" : " + stringNull.equals(stringRed));

        // Comparing two strings by ignoring case using String.equals() method
        tv.setText(tv.getText() + "\n\nString.equalsIgnoreCase() " + stringRed + " = "
                + stringRED + " : " + stringRed.equalsIgnoreCase(stringRED));


        // Compare two strings using == opeartor
        boolean result=false;
        if(stringRed==stringRed)
        {
            result = true;
        }
        tv.setText(tv.getText() +"\n\n(== operator) " +stringRed + " = " + stringRed2 + " : " + result);

        // Best way to compare null strings
        boolean result2 = false;
        if(stringNull == stringNull2)
        {
            result2=true;
        }
        tv.setText(tv.getText() + "\n(== operator) null = null" + " : " + result2);

        // Compare two strings using String.compareTo() method
        boolean result3=false;
        if(stringRed.compareTo(stringRed2)==0)
        {
            // If the two strings are exactly same then String.compareTo() method return zero
            result3 = true;
        }
        tv.setText(tv.getText() +"\n\nString.compareTo() " +stringRed + " = " + stringRed2 + " : " + result3);

        // Compare two strings using String.compareTo() method
        boolean result4=false;
        if(stringRed.compareTo(stringRED)==0)
        {
            // If strings are equal then return zero
            result4 = true;
        }
        tv.setText(tv.getText() +"\nString.compareTo() " +stringRed + " = " + stringRED + " : " + result4);
    }
}
More android examples