Created
April 22, 2021 16:04
-
-
Save Ingco/ad59ef1636a64cf526d9029d725b373f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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