substring(int beginindex, int endindex) in JAVA – JAVA 10th Class notes – ICSE JAVA Notes

substring(int beginindex, int endindex) in JAVA – JAVA 10th Class notes – ICSE JAVA Notes

substring(int beginindex, int endindex) :

substring(int beginindex, int endindex) function is used to extract a part of the string. This extracted part of the main string is known as the Substring.

Syntax : String var=str.substring(index);

Example : String str= “COMPUTER”;

String var=str.substring(3);

 

Output : PUTER

Here it displays all the character from 3rd position till end of the String.

 

Example : String str= “COMPUTER”;

String var=str.substring(3,6);

 

Output : PUT

Here it displays only a part of the string from 3rd position to the 6th position by excluding the character available at the 6th position.

 

/* Program to illustrate the function substring() */

 

import java.io.*;

class Sub_String

{

public static void main(String args[])throws IOException

{

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.print(” Enter a String : “);

String str=br.readLine();

System.out.println(” Enter the Start Index and the End Index for the Sub String : “);

int start=Integer.parseInt(br.readLine());

int end=Integer.parseInt(br.readLine());

String res=str.substring(start,end);

System.out.println(” Sub String is : ” + res);

}

}

Leave a Reply

Your email address will not be published. Required fields are marked *

Security Code: