
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How do we make my string comparison case insensitive in java?
We can compare Strings in Java in various ways −
Using the comapareTo() method − The compareTo() method compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string.
Example
import java.util.Scanner; public class StringComparison { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter string1: "); String str1 = sc.next(); System.out.println("Enter string2: "); String str2 = sc.next(); int result = str1.compareTo(str2); if (result < 0) { System.out.println("str1 is not equal to str2"); } else if (result == 0) { System.out.println("str1 is equal to str2"); } else { System.out.println("str1 is not equal to str2"); } } }
Output1
Enter string1: Hello Enter string2: Hello str1 is equal to str2
Output2
Enter string1: hello Enter string2: hi str1 is not equal to str2
Using the == operator − You can compare two strings using == operator. But, it compares references of the given variables not values.
Example
import java.util.Scanner; public class StringComparison { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str1 = "hello"; String str2 = "hello"; if (str1 == str2 ){ System.out.println("Both are equal"); } else { System.out.println("Both are not equal"); } } }
Output
Both are equal
Using the equals() method − of the String class accepts a String as parameter and it compares the current string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object including case.
Example
import java.util.Scanner; public class StringComparison { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter string1: "); String str1 = sc.next(); System.out.println("Enter string2: "); String str2 = sc.next(); boolean bool = str1.equals(str2); if (bool) { System.out.println("Both are equal"); } else { System.out.println("Both are not equal"); } } }
Output1
Enter string1: Hello Enter string2: hello Both are not equal
Output2
Enter string1: Hello Enter string2: Hello Both are equal
String comparison case insensitive
The equalsIgnoreCase() method of the String class is similar to the equals() method the difference if this method compares the given string to the current one ignoring case.
Example
import java.util.Scanner; public class StringComparison { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter string1: "); String str1 = sc.next(); System.out.println("Enter string2: "); String str2 = sc.next(); boolean bool = str1.equalsIgnoreCase(str2); if (bool) { System.out.println("Both are equal"); } else { System.out.println("Both are not equal"); } } }
Output1
Enter string1: Hello Enter string2: hello Both are equal
- Related Articles
- How do I make my string comparison case insensitive?
- How do I do a case insensitive string comparison in Python?
- How to do case insensitive string comparison of strings in JavaScript
- Case-insensitive string comparison in C++
- How do I make case-insensitive queries on MongoDB?
- How to make SQL case sensitive string comparison in MySQL?
- How to do case-sensitive string comparison in JavaScript?
- Case sensitive string comparison in Java.
- How to make a case-insensitive query in MongoDB?
- How to make jQuery attribute selector case insensitive?
- How to check if a String contains another String in a case insensitive manner in Java?
- How to test if a Java String contains a case insensitive regex pattern
- Case-insensitive string replacement using Python Program
- How do we check if a String contains a substring (ignoring case) in Java?
- How MySQL can perform case-sensitive string comparison?
