Page 240 - computer science (868) class 11
P. 240
The concat(String str) Function
This function joins two strings together. The string of the current String variable whose method is called and the string
mentioned as the parameter are the two strings that are joined. This function also returns a string. The syntax is:
String <variable> = String_datatype_Variable.concat(String str);
Let us see the below example:
String str1= "India-";
String str2= "New Delhi";
String together= str1.concat(str2);
System.out.println("The joined string is: " +together);
Output:
The joined string is: India-New Delhi
Note: We can also use the “+” operator to concatenate two strings.
For example:
String str1= "India-";
String str2= "New Delhi";
String together= str1 + str2;
System.out.println("The joined string is:" +together);
Output:
The joined string is: India-New Delhi
The substring (int index) Function
This function extracts all the characters from the index position till the end of the string. It returns a string, which is a
substring of the given string. The syntax is:
String <variable> = String_datatype_Variable.substring(int index);
Let us see the below example:
String str= "India - New Delhi";
String seperate= str.substring(8);
System.out.println("The Extracted string is: " +seperate);
Output:
The Extracted string is: New Delhi
The substring (int startindex, int endindex) Function
This function extracts all the characters from the index position ‘startindex’ till 1 less than the ‘endindex’. It always
returns a string, which is also a substring of the original string. The syntax is:
String <variable> = String_datatype_Variable.substring(int startindex, int endindex);
Let us see the below example:
String str= "India - New Delhi";
String removestr= str.substring(8,11);
System.out.println("The Extracted string is: " + removestr);
Output:
The Extracted string is: New
The replace (char chartoreplace, char chartoinsert) Function
This function replaces all the characters mentioned in ‘chartoreplace’ with the characters given in ‘chartoinsert’ and
creates a new String. The syntax is:
String <variable> = String_datatype_Variable.replace(char chartoreplace, char chartoinsert);
238238 Touchpad Computer Science-XI

