Page 250 - Computer science 868 Class 12
P. 250

Output:
                 The strings are same
                15. int compareTo(String st): This method compares the given string in the parameter with the string in the current
                 object alphabetically. It returns an integer value, i.e., 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 returns a negative value.
                 Comparison is based on the first string.
                 if st1>st2 : it returns a positive value
                 if st1<st2 : it returns a negative value
                 if st1==st2: it returns 0
                      String st1= "America";
                      String st2= "And";
                      int value= st1.compareTo(st2);
                      System.out.println("Value : "+ value);
                 Output:
                 Value : -1
                 (i.e., the difference between the ASCII codes of m and n = 110-111=-1, The comparison is between m and n.)

                16. int compareToIgnoreCase(String st): This method is same as the compareTo() method. Only the difference is that
                 it compares two strings without checking the case of the letters. It returns an integer-type value.

                      String st1= "India";
                      String st2= "INDIA";
                      int value= st1.compareToIgnoreCase(st2);
                      System.out.println("Value : "+ value);
                 Output:
                 Value : 0
                17. boolean endsWith(String st): This method checks whether the string in the current object ends with the string in
                 the parameter. This function returns a boolean type value.

                      String st1= "India is my country";
                      String st2= "try";
                      boolean b=st1.endsWith(st2);
                      if(b)
                      System.out.println(st1+ " ends with " + st2 );
                      else
                      System.out.println(st1+ " does not end with " + st2);
                Output:
                 India is my country ends with try
                18. boolean startsWith(String st): This method checks whether the string in the current object starts with the string in
                 the parameter. This function returns a boolean type value.

                      String st1= "India is my country";
                      boolean b=st1.endsWith("MY");
                      if(b)
                      System.out.println(st1+ " begins with MY");
                      else
                      System.out.println(st1+ " does not begin with MY");
                 Output:
                 India is my country does not begin with MY


                248248  Touchpad Computer Science-XII
   245   246   247   248   249   250   251   252   253   254   255