removeWrappingChar method
Returns a new string with char removed from the beginning and/or end.
When trimFirst is true (default), the string is trimmed before
checking. Matching and cutting are both UTF-16 code-unit based, so a
char that is an emoji or spans several code units is removed exactly.
Example:
'"abc"'.removeWrappingChar('"'); // 'abc'
'👋abc👋'.removeWrappingChar('👋'); // 'abc'
Audited: 2026-06-12 11:26 EDT
Implementation
@useResult
String removeWrappingChar(String char, {bool trimFirst = true}) {
String str = trimFirst ? trim() : this;
// char.length is a code-unit count (matching startsWith/endsWith), so cut
// with code-unit substring; the grapheme-indexed substringSafe used before
// skipped two graphemes for an emoji char and kept text after astral input.
if (str.startsWith(char)) {
str = str.substring(char.length);
}
if (str.endsWith(char)) {
str = str.substring(0, str.length - char.length);
}
return str;
}