Home >>Java String Methods >Java String endsWith() method
Java endsWith() method in java string is used to checks whether a given suffix ends this string. If this string ends with a specified suffix otherwise returns false, it returns true.
public boolean endsWith(String suffix)
{
return startsWith(suffix, value.length - suffix.value.length);
}
public boolean endsWith(String suffix)
suffix - Used to specify the Sequence of character
true or false
public class EndsWithExample
{
public static void main(String args[])
{
String n1="Welcome to phptpoint";
System.out.println(n1.endsWith("t"));
System.out.println(n1.endsWith("point"));
}
}
public class EndsWithExample2
{
public static void main(String[] args)
{
String strln1 = "This is phptpoint.com";
System.out.println(strln1.endsWith("nt"));
if(strln1.endsWith(".com"))
{
System.out.println("end with .com");
}
else System.out.println("not end with .com");
}
}
The method contain() is useful in finding a char-sequence in the string. We can use it to generate search based results in a control structure.
public class ContainsExample3
{
public static void main(String[] args)
{
String strl2 = " Welcome to Phptpoint.com";
if(strl2.contains("Phptpoint.com"))
{
System.out.println("Visit Phptpoint.com");
}
else
{
System.out.println("Result not found");
}
}
}