JAVA program for string method substring(int, int)
This JAVA program is for string method substring(int, int).
String method substring(int, int) returns a sub-string of a string object starting and ending from indices mentioned in the brackets .The index mentioned in the second parameter is excluded.
For example string str = ‘coding’ then str.substring(1,5) will return ‘odin’.
Dry Run of the Program
Take input ‘str’.Let us take input str=’CODEDOST’
str.substring(0, 4) will return the string starting from 4th index ‘CODE’
Program
import java.util.*; class substring2 { 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 on using substring(int, int) = "+str.substring(0,4)); } }