safeSubstring method

String safeSubstring(
  1. int start, [
  2. int? end
])

安全的子字符串提取

Implementation

String safeSubstring(int start, [int? end]) {
  if (isEmpty || start >= length) return '';
  final safeStart = start.clamp(0, length);
  final safeEnd = end?.clamp(safeStart, length) ?? length;
  return substring(safeStart, safeEnd);
}