Page 249 - Computer science 868 Class 12
P. 249
10. String substring(int startindex, int endindex): This method is used for extracting all the characters from the index
position startindex till the endindex-1. It returns a String.
String st = "We study in class 12";
String extractst = st.substring(12,17);
System.out.println("The Extracted string is :" +extractst);
Output:
The Extracted string is: class
11. String replace (char chtorep, char chtoadd): This method replaces all the characters in chtorep with the character
chtoadd and creates a new String.
String st = "We study in class 12";
String replacest = st.replace('s', 'S');
System.out.println("The replaced string is :" +replacest);
Output:
The replaced string is : We Study in claSS 12
12. String replace (String stringtorep, String stringtoadd): This method replaces all the sequence of characters in
stringtorep with the sequence of characters in stringtoadd and creates a new String. It replaces all the sets having
the same sequence.
String st = "We study in class 12";
String replacest = st.replace("study","read");
System.out.println("The replaced string is :" +replacest);
Output:
The replaced string is : We read in class 12
13. boolean equals(String st): This method checks whether the string of the current string object whose method is
called is the same as the string st in the parameter. It returns true if both the strings are equal, else returns false.
This function is case-sensitive.
String st1 = "COMPuter";
String st2 = "computer";
boolean check = st1.equals(st2);
if(check)
System.out.println("Same");
else
System.out.println("Are not same");
Output:
Same
14. boolean equalsIgnoreCase(String st): This method is similar to equals() which checks whether the string of the
current string object whose method is called is the same as the string st 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 returns
false.
String st1= "COMPuter";
String st2= "computer";
boolean check= st1.equalsIgnoreCase(st2);
if(check)
System.out.println("The strings are same");
else
System.out.println("The strings are not same");
247
Methods 247

