Home >>Java String Methods >Java String endsWith() method

Java String endsWith() method

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.

Internal implementation


public boolean endsWith(String suffix) 
{  
    return startsWith(suffix, value.length - suffix.value.length);  
 }  

Signature

public boolean endsWith(String suffix)

Parameter

suffix - Used to specify the Sequence of character

Returns

true or false

String endsWith() method example 1

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"));  
}
}

Output
true
true
String EndsWith() Method Example 2

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");  
    }  
}  

Output
false
end with .com
String contains() Method Example 3

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");
		}		
    }  
}  

Output
Visit Phptpoint.com