Page 423 - CA_Blue( J )_Class10
P. 423

You will get the following output:
                 Value: 1
                 Since, str1 has one character extra than str2. Note that if str1= INDIA and str2=INDIAN, then compareTo() would have
                 returned -1.
                 Example 3:

                    String str1= "INDIA";
                    String str2= "INDIA";
                    int value= str1.compareTo(str2);
                    System.out.println("Value: "+ value);
                 You will get the following output:
                 Value: 0
                 Since, both the string are equal.

                 16.3.13 The compareToIgnoreCase() Method
                 The compareToIgnoreCase() method is same as the compareTo() method. The only difference is that it compares two
                 strings without checking the case of the letters. It returns integer type value. The syntax to use the compareToIgnoreCase()
                 method is:

                    int <variable> = <String_Object>.compareToIgnoreCase(String str);
                 For example:

                    String str1= "India";
                    String str2= "INDIA";
                    int value= str1.compareToIgnoreCase(str2);
                    System.out.println("Value: "+ value);
                 You will get the following output:
                 Value: 0

                 16.3.14 The startsWith() Method
                 The startsWith() method checks whether the string in the current String object starts with the string in the parameter.
                 This method returns a boolean value either true or false. The syntax to use the startsWith() method is:
                    boolean  <variable> = <String_Object>.startsWith(String str);
                 For example:

                    String str1= "India is my country";
                    String str2= "dia";
                    boolean b=str1.startsWith(str2);
                    if(b)
                        System.out.println(str1+ " begins with " + str2);
                    else
                        System.out.println(str1+ " does not begin with " + str2);
                 You will get the following output:
                 India is my country does not begin wit  h dia

                 16.3.15 The endsWith() Method
                 The endsWith() method checks whether the string in the current String object is ending with the string in the parameter.
                 This method returns a boolean value either true or false. The syntax to use the endsWith(String str) method is:
                    boolean <variable> = <String_Object>.endsWith(String str);
                 For example:
                    String str1= "India is my country";

                                                                                                                       421
                                                                                                       String Handling  421
   418   419   420   421   422   423   424   425   426   427   428