JAVA program for string method substring(int)

JAVA program for string method substring(int)

This JAVA program is for string method substring(int).

String method substring(int) returns a sub-string of a string object starting from index mentioned in the brackets.

For example string str = ‘coding’ then str.substring(2) will return ‘ding’.

Dry Run of the Program

Take input ‘str’.Let us take input str=’CODEDOST’

str.substring(4) will return the string starting from 4th index ‘DOST’

Program

import java.util.*;

class substring1
{
	public static void main(String args[])
	{
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter the string");
		String str = sc.nextLine();
		
		System.out.println("The string returned statring from 4th position = "+str.substring(4));
	}
}

Output

Share Me!