removeFirstLastChar method
Returns a new string with both the first and last characters removed. Audited: 2026-06-12 11:26 EDT
Implementation
@useResult
String removeFirstLastChar() {
// Count and slice by grapheme (like removeLastChars above), not code-unit
// `length`: feeding a code-unit length into the grapheme-indexed
// substringSafe removed the wrong span for astral content
// (e.g. 'a😀b'.removeFirstLastChar() returned '😀b' instead of '😀').
final int graphemeCount = characters.length;
return graphemeCount < 2 ? '' : substringSafe(1, graphemeCount - 1);
}