Computer Applications

How does endsWith() and startsWith() differ? Explain with an example.

Java String Handling

25 Likes

Answer

endsWith() tests if the string object ends with the string specified as its argument. startsWith() tests if the string object starts with the string specified as its argument. Consider the below example:


public class Example {
  public static void main(String args[]) {
    String str = "ICSE Computer Applications";
    System.out.println("Does " + str + " starts with ICSE? " + str.startsWith("ICSE"));
    System.out.println("Does " + str + " ends with tions? " + str.endsWith("tions"));
  }
}


Here, both str.startsWith("ICSE") and str.endsWith("tions") returns true as str starts with "ICSE" and ends with "tions".

Answered By

12 Likes


Related Questions