Skip to content

Instantly share code, notes, and snippets.

@Ingco
Created April 22, 2021 16:04
Show Gist options
  • Save Ingco/ad59ef1636a64cf526d9029d725b373f to your computer and use it in GitHub Desktop.
Save Ingco/ad59ef1636a64cf526d9029d725b373f to your computer and use it in GitHub Desktop.
extension StringExtension on String {
/// Truncate a string if it's longer than [maxLength] and add an [ellipsis].
String truncate(int maxLength, [String ellipsis = "…"]) => length > maxLength
? '${substring(0, maxLength - ellipsis.length)}$ellipsis'
: this;
}
void main() {
print('lsdkf osfk sifhsoidfj'.truncate(10));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment