textSubString method

String textSubString(
  1. int strLength
)

strLength 為想要取的字串長度,若超過則後面的字串會用「...」取代顯示

Implementation

String textSubString(int strLength) {
  String text = trim();
  // 若實際長度小於等於判斷長度,則直接返回
  if (text.length <= strLength) {
    return text;
  }
  int textLength = text.length;
  text = text.substring(0, text.length < strLength ? text.length : strLength);
  if (textLength > strLength) text = "$text...";
  return text;
}