How does endsWith() and startsWith() differ? Explain with an example.
Advertisements
Question
How does endsWith() and startsWith() differ? Explain with an example.
Code Writing
Advertisements
Solution
starts With() returns true if a provided string is used as a prefix for another string, and false otherwise.
Example1:
Strings 1 = "Monopoly";
String s2 = "Mono";
System.out.println(s1.starts With(s2));
Output: true
endsWith(): It returns true if a given string is used as a suffix to another string, false otherwise.
Example2:
String s1 = "Monopoly";
String s2 = "poly";
System.out.println(s1.endsWith(s2));
Output: true shaalaa.com
Is there an error in this question or solution?
Chapter 4: String Handling - EXERCISES [Page 289]
