highlightSubstring method
Wraps each occurrence of substring with before and after.
Matching is case-sensitive. Overlapping matches are not merged.
substring must not be empty.
Returns a new string with each occurrence of substring wrapped.
Throws ArgumentError if substring is empty.
Example:
'hello world'.highlightSubstring(substring: 'o', before: '<', after: '>'); // 'hell<o> w<o>rld'
Implementation
@useResult
String highlightSubstring({
required String substring,
required String before,
required String after,
}) {
if (substring.isEmpty) {
throw ArgumentError(_kErrSubstringNotEmpty, _kParamSubstring);
}
if (isEmpty) return this;
return replaceAll(substring, '$before$substring$after');
}