replaceLast method

String replaceLast(
  1. String pattern,
  2. String replacement
)

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) {
  if (this == null) {
    throw ArgumentError('String is null');
  }
  int index = this!.lastIndexOf(pattern);
  if (index == -1) {
    return this!;
  }
  return this!.replaceRange(index, index + pattern.length, replacement);
}