I am trying to implement String.endsWith()
method without the use of the built-in method String.endsWith()
.
Here is my code:
public static boolean endsWithSuffix(String str, String suffix) {
char[] chStr = str.toCharArray();
char[] chSuf = suffix.toCharArray();
if(!str.contains(suffix) {
return false;
}
return true;
}
What is the best way to implement the endsWith()
method without using the built-in method?
I've been trying with character arrays but no luck yet. Would strings be easier?
One way would be like this:
suffix <= str
.n
characters of str
where n = suffix.length()
, using substring()
.suffix
with equals()
or equalsIgnoreCase()
.