ICSE Class 10 Computer Applications Question 14 of 46

String Handling — Question 14

Back to all questions
14
Question

Question 3(iii)

Differentiate between startsWith() and endsWith().

Answer
startsWith()endsWith()
startsWith() tests if the string object starts with the string specified as its argument.endsWith() tests if the string object ends with the string specified as its argument.
Example:
String str = "ICSE Computer Applications";
boolean res = str.startsWith("ICSE");
System.out.println(res);

The output of this code is true as str begins with "ICSE".
Example:
String str = "ICSE Computer Applications";
boolean res = str.endsWith("tions");
System.out.println(res);

The output of this code is true as str ends with "tions".