replaceBefore method

String? replaceBefore(
  1. String delimiter,
  2. String replacement, [
  3. String? defaultValue
])

Replace part of string before the first occurrence of given delimiter with the replacement string. If the string does not contain the delimiter, returns missingDelimiterValue? which defaults to the original string.

Implementation

String? replaceBefore(String delimiter, String replacement, [String? defaultValue]) {
  if (this == null) return null;
  final index = this!.indexOf(delimiter);
  return (index == -1)
      ? defaultValue.isEmptyOrNull
          ? this
          : defaultValue
      : this!.replaceRange(0, index, replacement);
}