Page 304 - ComputerScience_Class_11
P. 304
The equalsIgnoreCase(String str) Function
Like equals() method, this method also checks whether the value of the current string object is the same as the string
str in the parameter. But the difference is that it is not case-sensitive. It returns true if both the strings are equal, even
if they are of different cases, else it returns false. The syntax is:
boolean <variable> = String_datatype_Variable.equalsIgnoreCase(String str);
Let us see the below example:
Write a program to compare two strings ignoring case sensitivity and print whether they are
Program 15
equal or not.
1 class equal
2 {
3 public static void main(String[] args)
4 {
5 String wd1= "India";
6 String wd2= "INDIA";
7 boolean eq= wd1.equalsIgnoreCase(wd2);
8 if(eq==true)
9 System.out.println("The strings "+ wd1 + " and " + wd2 + " are equal ");
10 else
11 System.out.println("The strings "+ wd1 + " and " + wd2 + " are not equal ");
12 }
13 }
The output of the preceding program is as follows:
BlueJ: Terminal Window - Java
Options
The strings India and INDIA are equal
The compareTo(String str) Function
This function compares the given string in the parameter with the string in the current object alphabetically. It returns
an integer type value, i.e., the value of the difference between the ASCII codes of the characters that are compared.
If both strings are equal, it returns 0. If the first string is larger lexicographically than the second string, it returns a
positive number else it returns a negative value.
Note: The ASCII value range is:
1. A to Z: 65 to 90
2. a to z: 97 to 122
3. 0 to 9: 48 to 57
The comparison is based on the first string.
if str1>str2 : it returns a positive value
302 Touchpad Computer Science (Ver. 3.0)-XI

