removeLastOccurrence method
Removes the last occurrence of target
from the string.
Implementation
String removeLastOccurrence(String target) {
// Find the last index of the target.
final int lastIndex = lastIndexOf(target);
// If the target is not found, return the original string.
if (lastIndex == -1) return this;
// Reconstruct the string without the last occurrence.
return substring(0, lastIndex) + substring(lastIndex + target.length);
}