safeSubstring method

String safeSubstring(
  1. int startIndex,
  2. int endIndex
)

安全地截取字符串,如果字符串长度不够,则返回原字符串。

例如:

'12345678'.safeSubString(0, 20); // '12345678'

Implementation

String safeSubstring(int startIndex, int endIndex) {
  if (startIndex < 0) startIndex = 0;
  if (endIndex > length) endIndex = length;
  return substring(startIndex, endIndex);
}