This program accepts the string, prefix string and suffix string from the keyboard and checks whether the string starts with given prefix and ends with given suffix or not.
import java.io.*;
/**
*
* @author JavaHotSpot
*/
public class stringPrefixSuffix
{
public static void main(String args[])
{
String s1,s2,s3;
DataInputStream d;
try
{
System.out.println("Enter the String to be checked");
d=new DataInputStream(System.in);
s1=(String)d.readLine();
System.out.println("Enter the prefix");
s2=(String)d.readLine();
System.out.println("Enter the suffix");
s3=(String)d.readLine();
if(s1.startsWith(s2))
{
System.out.println("The given string start with prefix "+s2);
}
else
{
System.out.println("The given string not start with prefix "+s2);
}
if(s1.endsWith(s3))
{
System.out.println("The given string start with suffix "+s3);
}
else
{
System.out.println("The given string not start with suffix "+s3);
}
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}
Output:
Run 1
Enter the String to be checked
JavaHotSpot
Enter the prefix
Java
Enter the suffix
HotSpot
The given string start with prefix Java
The given string start with suffix HotSpot
Run 2:
Enter the String to be checked
JavaHotSpot
Enter the prefix
HotSpot
Enter the suffix
Java
The given string not start with prefix HotSpot
The given string not start with suffix Java
Related Articles:
Java Programming Example of how to extract substring from the given string
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 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 extract substring from the given string
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 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?