replaceLast method

String replaceLast(
  1. String oldValue,
  2. String newValue
)

Replaces the last occurrence of oldValue with newValue.

Example:

'hello hello'.replaceLast('hello', 'hi'); // 'hello hi'

Implementation

String replaceLast(String oldValue, String newValue) {
  final index = lastIndexOf(oldValue);
  if (index == -1) return this;
  return '${substring(0, index)}$newValue${substring(index + oldValue.length)}';
}