Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
t = s.removesuffix(w)
removesuffix is in Python 3.9+
const t = s.endsWith(w) ? s.slice(0, -w.length) : s
i = index(s,w,back=.true.) - 1
if (i == len(s) - len(w) ) then
allocate (t, source=s(:i))
else
allocate (t, source=s)
end if
This assumes that t is unallocated before.
String t = s;
int index = 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.
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.
int i = s.lastIndexOf(w),
m = s.length(), n = w.length();
t = i == m - n ? s.substring(0, i) : s;
local t = s:gsub(w.."$", "")
The $ attached to w matches the end of the string s and is replaced by an empty string.
if (length $s == rindex($s, $w) + length $w) {
my $t = substr $s, 0, rindex $s, $w;
}