replaceFirst method

String replaceFirst(
  1. String from,
  2. String replace
)

Replaces the first occurrence of the substring from with replace. If from is not found in the string, returns the original string.

Example:

print('Hello World'.replaceFirst('World', 'There')); // Output: 'Hello There'

Implementation

String replaceFirst(String from, String replace) {
  int pos = indexOf(from);
  return pos < 0 ? this : '${substring(0, pos)}$replace${substring(pos + from.length)}';
}