Java-Startswith and Endswith

Startswith and Endswith

The String class provides two more methods that are specialized versions of the equals( ) method. The startWith( ) method enables us to check whether a given string str1 starts with another string str2. For example, in the following program, the startWith( ) method will return true as the string “hajsoftutorial” starts with the string “haj”.

The endWith( ) method helps us to check if a given string str1 ends with another string str2. For example, in the following program, the string “hajsoftutorial” ends with the string “rial”, the endWith method will return true.

Program


1

Program Source

public class Javaapp {
    
    public static void main(String[] args){
        
        String str1 = new String("hajsoftutorial");
        
        if(str1.startsWith("haj"))
        {
            System.out.println("str1 start with haj");
        }
        
        if(str1.endsWith("ial"))
        {
            System.out.println("str1 end with ial");
        }
   }
}

Leave a Comment