Stringt= s;
intindex= s.lastIndexOf(w);
if (index + w.length() == s.length()) {
t = s.substring(0, index);
}
Java does not have a native String method for this.
s.lastIndexOf(w) gives the index where w can be found in s starting at the end of s and moving towards the beginning, or -1 if it isn’t there.
preg_replace("/{$w}$/u", '', $s);
We can't use rtrim($w, $s), because we want to remove exact suffix value, if it matches. rtrim() would remove all characters from $s located at the end of $s in any order.