Compare two strings in Java Android without case sensitive example

How to check two strings is equal or not using equalsIgnoreCase() method and result show on screen.

In this tutorial we are comparing two string variables value using equalsIgnoreCase() , which will automatically ignore the case sensitiveness from strings. So here is the complete step by step tutorial for Compare two strings in Java Android without case sensitive example.

android-project-download-code-button

How to Compare two strings in Java Android without case sensitive example.

Code for MainActivity.java file.

 package com.comparestringswithoutcasesensitive_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 = "HELLO" ;
 
 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.equalsIgnoreCase(string2);
 
 textview3.setText("Result = " + 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.comparestringswithoutcasesensitive_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 without case sensitive example

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

Leave a Reply

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