Page 351 - Computer science 868 Class 12
P. 351
8. String concat(String str): This method joins two strings together. The string of the current Stringobject whose
method is called and the string in the parameter are the two strings that are joined. This method returns a string.
Syntax: String <variable> = Stringobject.concat(String str);
Example:
String str1 = "Computer";
String str2 = "Science";
String joinstr = str1.concat(str2);
System.out.println("The joined string is :" +joinstr);
Output:
The joined string is: Computer Science
Note: We can also use the “+” operator to concatenate two strings.
For Example:
String str1= "Kolkata-";
String str2= "700114";
String joinstr= str1 + str2;
System.out.println("The joined string is :" +joinstr);
Output:
The joined string is : Kolkata–700114
9. String substring(int index): This method is used for extracting all the characters from the index position till the end
of the string. It returns a String.
Syntax: String <variable> = Stringobject.substring(int index);
Example:
String str = "Computer Science – Class XII";
String extractstr = str.substring(9);
System.out.println("The Extracted string is :" +extractstr);
Output:
The Extracted string is: Science – Class XII
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.
Syntax: String <variable> = Stringobject.substring(int startindex, int endindex);
Example:
String str = "Computer Science – Class XII";
String extractstr= str.substring(9,16);
System.out.println("The Extracted string is :" +extractstr);
Output:
The Extracted string is: Science
11. String replace(char chtorep, char chtoadd): This method replaces all the characters in chtorep with the character
chtoadd and creates a new String.
Syntax: String <variable> = Stringobject.replace(char chtorep, char chtoadd);
Example:
String str = "Rimi is good in studies";
String replacestr= str.replace('i', 'a');
System.out.println("The replaced string is :" +replacestr);
Output:
The replaced string is: Rama as good an studaes
349
Strings 349

