Page 421 - CA_Blue( J )_Class10
P. 421

method is used to extract all the characters from the begin index to the end index, excluding the last character. It
                 returns a String value. The syntax to use this method is:
                    String <variable> = <String_Object>.substring(int startindex, int endindex);
                 For example:
                    String str= "Kolkata-700114";
                    String extractstr= str.substring(8,11);
                    System.out.println("The extracted string is: " +extractstr);
                 You will get the following output:
                 The extracted string is: 700

                 16.3.9 The replace () Method
                 The replace() method replaces all the occurrences of a character in the string with the provided character. It takes a
                 character that is to be replaced and the character with which the old character is replaced. It returns a string value
                 after replacing the character. The syntax to use the replace method is:
                    String <variable> = <String_Object>.replace(char chtorep, char chtoadd);
                 For example:
                    String str= "Computer Applications";
                    String replacestr= str.replace('o', 'a');
                    System.out.println("The replaced string is: " +replacestr);
                 You will get the following output:
                 The replaced string is: Camputer Applicatian

                 The replace() has an overloaded method which replaces a sequence of characters with another sequence of characters
                 in the string and creates a new String. The syntax to use this method is:

                    String <variable> = <String_Object>.replace(String stringtorep, String stringtoadd);
                 For example:

                    String str= "bad students get bad marks";
                    String replacestr= str.replace("bad", "good");
                    System.out.println("The replaced string is: " +replacestr);
                 You will get the following output:
                 The replaced string is: good students get good marks

                 16.3.10 The equals() Method
                 The equals() method checks whether the current String object whose method is called is same as the String value
                 passed as parameter. It returns true if both the strings are equal otherwise returns false. This method is case-sensitive
                 which means it considers “Delhi” and “DELHI” as two different string values. The syntax to use the equals() method is:
                    boolean <variable> = <String_Object>.equals(String str);
                 For example:
                    String str1 = "India";
                    String str2 = "india";
                    boolean check = str1.equals(str2);
                    if(check)
                        System.out.println("The strings are equal.");
                    else
                        System.out.println("The strings are not equal.");
                 You will get the following output:
                 The strings are not equal.





                                                                                                                       419
                                                                                                       String Handling  419
   416   417   418   419   420   421   422   423   424   425   426