replaceFirstN method
Replaces the first n occurrences of pattern with replacement.
If n is 0 or negative, returns this string unchanged. If n is null or omitted, replaces all.
pattern must not be empty.
Throws ArgumentError if pattern is empty.
Example:
'a-b-c'.replaceFirstN('-', '_', 2); // 'a_b_c'
'a-b-c'.replaceFirstN('-', '_', 1); // 'a_b-c'
Implementation
@useResult
String replaceFirstN(String pattern, String replacement, [int? n]) {
if (pattern.isEmpty) {
throw ArgumentError(_kErrPatternNotEmpty, _kParamPattern);
}
if (n != null && n <= 0) return this;
if (isEmpty) return this;
int count = 0;
final int? limit = n;
String result = this;
int index = result.indexOf(pattern);
while (index >= 0 && (limit == null || count < limit)) {
result = result.replaceRange(index, index + pattern.length, replacement);
count++;
index = result.indexOf(pattern, index + replacement.length);
}
return result;
}