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
Write a statement each to perform the following task on a string:
Find and display the position of the last space in a string s.
Write a statement each to perform the following task on a string:
Convert a number stored in a string variable x to double data type.
Describe the purpose and write the syntax of toUpperCase() function.
Describe the purpose and write the syntax of trim() function.