replaceLast method

String replaceLast(
  1. Pattern from,
  2. String to
)

Creates a new string with the last occurrence of from replaced by to.

Finds the last match of from in this string and creates a new string where that match is replaced with the to string.

Example:

'0.0001'.replaceLast(RegExp(r'0'), ''); // '0.001'
'0.0001'.replaceLast(RegExp(r'0'), '1'); // '0.0011'

Implementation

String replaceLast(Pattern from, String to) => from.allMatches(this).tryLast.map((match) => replaceRange(match.start, match.end, to)).or(this);