This program accepts the string, starting index and ending index from the keyboard and extract the substring from the given string according to index.
import java.io.*;
/**
*
* @author JavaHotSpot
*/
public class substring
{
public static void main(String args[])
{
String s1,s2,s3,s4;
int st,ed;
DataInputStream d;
try
{
System.out.println("Enter the String");
d=new DataInputStream(System.in);
s1=(String)d.readLine();
System.out.println("Enter the Starting Index");
s2=(String)d.readLine();
st=Integer.parseInt(s2);
System.out.println("Enter the Ending Index");
s3=(String)d.readLine();
ed=Integer.parseInt(s3);
s4=s1.substring(st, ed);
System.out.println("String is = "+s1+" Substring is ="+s4);
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}
Output:
Enter the String
JavaHotSpot
Enter the Starting Index
3
Enter the Ending Index
8
String is = JavaHotSpot Substring is =aHotSp
Related Articles:
Java Programming Example of how to remove the extra spaces from the given string
Java Programming Example of how to convert string into bytes
Java Programming Example of how to convert String into characters and display the character?
Java Programming Example of how to replace old character with new character in the string
Java Programming Example to check whether the strings starts with given prefix and ends with given suffix or not?
Java Programming Example of finding the length of the given string
Java Programming Example of concatenation of two strings
Java Programming Example of converting all characters in the string upper case and vise versa
Java Programming Example to check whether the two strings are equal or not?
Related Articles:
Java Programming Example of how to remove the extra spaces from the given string
Java Programming Example of how to convert string into bytes
Java Programming Example of how to convert String into characters and display the character?
Java Programming Example of how to replace old character with new character in the string
Java Programming Example to check whether the strings starts with given prefix and ends with given suffix or not?
Java Programming Example of finding the length of the given string
Java Programming Example of concatenation of two strings
Java Programming Example of converting all characters in the string upper case and vise versa
Java Programming Example to check whether the two strings are equal or not?