replaceFirst method

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

Replaces the first occurrence of oldValue with newValue.

Example:

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

Implementation

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