Compare two Strings in Java Android program with case sensitive example

How to check string variable contains another string variable value or not and return in true false.

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.

android-project-download-code-button

How to Compare two Strings in Java Android program with case sensitive example.

Code for MainActivity.java file.

 package com.comparetwostrings_android_examples.com;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;


public class MainActivity extends Activity {

 String string1 = "hello" ;
 String string2 = "HELLOW" ;
 
 Boolean result;
 
 TextView textview1,textview2,textview3;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 textview1 = (TextView)findViewById(R.id.textView1);
 textview2 = (TextView)findViewById(R.id.textView2);
 textview3 = (TextView)findViewById(R.id.textView3);
 
 textview1.setText(string1);
 textview2.setText(string2);
 
 result = string1.equals(string2);
 
 textview3.setText("Equal = " + result);
 
 
 }
}

Code for activity_main.xml layout file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.comparetwostrings_android_examples.com.MainActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:text="String1"
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center" />

 <TextView
 android:id="@+id/textView2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="23dp"
 android:text="String2"
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center" />

 <TextView
 android:id="@+id/textView3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView2"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="23dp"
 android:text="Result"
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center" />

</RelativeLayout>

Screenshot:

Compare two Strings in Java Android program with case sensitive example

Click here to download Compare two Strings in Java Android program with case sensitive example project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *