Page 303 - ComputerScience_Class_11
P. 303

5      String str= "Good Morning everyone, This is Morning news from ABC TV";
                   6      String replacestr= str.replace("Morning", "Evening");

                   7      System.out.println("The replaced string is:" +replacestr);
                   8      }
                   9      }


                 The output of the preceding program is as follows:
                       BlueJ: Terminal Window - Java

                   Options

                  The replaced string is: Good Evening everyone, This is Evening news from ABC TV

                 The equals(String str) Function
                 This function checks whether the string of the current string object is the same as the string str in the parameter. It
                 returns true, if both the strings are equal, else it returns false. This function is case-sensitive. The syntax is:

                    boolean <variable> = String_datatype_Variable.equals(String stringobject);
                 Let us see the below example:


                   Program 14    Write a program to compare two strings and print whether they are equal or not.

                   1      class equal
                   2      {

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

                   5      String word1= "School";
                   6      String word2= "School";

                   7      boolean eq= word1.equals(word2);
                   8      if(eq)

                   9      System.out.println(word1 + " and " + word2 + " are equal");
                  10      else

                  11      System.out.println(word1 + " is not same as " + word2);
                  12      }
                  13      }


                 The output of the preceding program is as follows:

                       BlueJ: Terminal Window - Java

                   Options
                  School is not same as SChool






                                                                                                           Strings  301
   298   299   300   301   302   303   304   305   306   307   308