subStringWithMaxLength method
根据最大字符限制截取字符串 例:xx...xx emoji表情按一个字符算,不截取
Implementation
String subStringWithMaxLength(int maxLen,
{int startLen = 2, int endLen = 2, String ellipsis = '...'}) {
if (length <= maxLen) {
return this;
}
int firstIndex = startLen;
if (startLen <= 0) {
firstIndex = 0;
} else {
int startChar = this[firstIndex].codeUnitAt(0);
if (isLowSurrogate(startChar)) {
firstIndex++;
}
}
int lastIndex = length - endLen;
if (endLen <= 0 || lastIndex < 0) {
lastIndex = length - 1;
} else {
int endChar = this[lastIndex].codeUnitAt(0);
if (isLowSurrogate(endChar)) {
lastIndex--;
}
}
if (lastIndex <= firstIndex) {
return this;
}
return substring(0, firstIndex) + ellipsis + substring(lastIndex);
}