replaceBeforeLast method

String replaceBeforeLast(
  1. String string,
  2. String replacement
)

Replace before the last occurrence of a string.

Implementation

String replaceBeforeLast(String string, String replacement) {
  if (this == null) {
    throw ArgumentError('string: $this');
  }
  if (this!.isEmpty) {
    return '';
  }
  final index = this!.lastIndexOf(string);
  if (index == -1) {
    return this!;
  }
  return replacement + this!.substring(index);
}