Page 241 - computer science (868) class 11
P. 241

Let us see the below example:
                    String str= "Rama is going to play cricket";
                    String replacechar= str.replace('i', 'a');
                    System.out.println("The replaced string is: " + replacechar);
                 Output:
                 The replaced string is: Rama as goang to play cracket


                 The replace(String stringtoreplace, String stringtoinsert) Function
                 This function replaces all the sequence of characters given in ‘stringtoreplace’ with the sequence of characters given
                 in ‘stringtoinsert’  and creates a new String. It replaces all the sets having the same sequence. The syntax is:

                    String <variable> = String_datatype_Variable.replace(String stringtoreplace, String
                    stringtoinsert);
                 Let us see the below example:
                    String str= "Good Morning everyone, This is Morning news from ABC TV";
                    String replacestr= str.replace("Morning", "Evening");
                    System.out.println("The replaced string is:" +replacestr);
                 Output:
                 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:
                    String word1= "School";
                    String word2= "SChool";
                    boolean eq= word1.equals(word2);
                    if(eq)
                        System.out.println(word1 + " and " + word2 + " are equal");
                    else
                        System.out.println(word1 + " is not same as  " + word2);
                 Output:
                 School is not same as SChool

                 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:
                    String wd1= "India";
                    String wd2= "INDIA";
                    boolean eq= wd1.equalsIgnoreCase(wd2);
                    if(eq==true)
                        System.out.println("The strings "+ wd1 + " and " + wd2 + " are equal ");
                    else
                        System.out.println("The strings "+ wd1 + " and " + wd2 + " are not equal ");
                 Output:
                 The strings India and INDIA are equal


                                                                                                                       239
                                                                                                              Strings  239
   236   237   238   239   240   241   242   243   244   245   246