Java String class comes with many string utility methods with which string manipulation becomes easier. Read these methods and you will remember the laborious days of C/C++. One such method is endsWith() String.
The present method endsWith(String str) checks the given string ends with a few characters like str or not. If ends with characters str, the method returns true else false.
The signature is given as defined in String class.
- public boolean endsWith(String str): Returns true if the given string ends with str, else false.
Following example on endsWith() String illustrates the usage of both the above methods.
public class StringMethodDemo
{
public static void main(String args[])
{
String str1 = "RajaRamMohan";
boolean b1 = str1.endsWith("Mohan");
System.out.println(str1 +" ends with Mohan: " + b1);
// one practical example
// to separate all the names ending with Rao
String names[] = { "SNRao", "Jyostna", "Satya Rao", "Prasad", "Sai Rao" };
System.out.println("\nExtracting names from array ending with \"Rao\":");
for(int i = 0; i < names.length; i++)
{
if(names[i].endsWith("Rao"))
{
System.out.print(names[i] + ", ");
}
}
}
}

Output screenshot on endsWith() String Example
A similar method exists in String class startsWith() String.