Page 306 - ComputerScience_Class_11
P. 306

The output of the preceding program is as follows:

                     BlueJ: Terminal Window - Java

                 Options
                Value: 1
                since, str1 has one character extra than str2.




                      Note:  if str1= “INDIA” and str2=”INDIAN”, then compareTo() will return-1.



                               Write a program to compare two identical strings and print the result using the compareTo
                Program 18
                               method.
                1       class compare
                2       {

                3       public static void main(String[] args)
                4       {

                5       String wd1= "Computer";
                6       String wd2= "Computer";

                7       int value= wd1.compareTo(wd2);
                8       System.out.println("Value: "+ value);

                9       }
                10      }


              The output of the preceding program is as follows:

                     BlueJ: Terminal Window - Java
                 Options

                Value: 0
                since, both the strings are equal.



              The compareToIgnoreCase(String str) Function
              This function is same as the compareTo() function. The only difference is that it compares the two strings without
              checking the case of the letters. It returns an integer type value. The syntax is:
                  int <variable> = String_datatype_Variable.compareToIgnoreCase(String str);
              Let us see the below example:
                               Write a program to compare two strings ignoring case sensitivity and print the result using
                Program 19
                               compareToIgnoreCase method.

                1       class compare{
                2       public static void main(String[] args)





                  304  Touchpad Computer Science (Ver. 3.0)-XI
   301   302   303   304   305   306   307   308   309   310   311