replaceFirst method

String replaceFirst(
  1. String pattern,
  2. String replacement
)

Returns a new String with the first occurrence of the given pattern replaced with the replacement String.

If the String is null, an ArgumentError is thrown.

Example

String s = "esentis".replaceFirst("s", "S"); // returns "eSentis";

Implementation

String replaceFirst(String pattern, String replacement) {
  int index = this.indexOf(pattern);
  if (index == -1) {
    return this;
  }
  return this.replaceRange(index, index + pattern.length, replacement);
}