replaceLast method
Returns a new String
with the last occurrence of the given pattern replaced with the replacement String
.
If the String
is null
, an ArgumentError
is thrown.
Example
String s = "esentis".replaceLast("s", "S"); // returns "esentiS";
Implementation
String replaceLast(String pattern, String replacement) {
int index = this.lastIndexOf(pattern);
if (index == -1) {
return this;
}
return this.replaceRange(index, index + pattern.length, replacement);
}