Last active
April 22, 2020 06:21
-
-
Save kherel/aa6fb66e99149f9f8858f03e88b311c3 to your computer and use it in GitHub Desktop.
how to short the text by cutting in the middle of the string
This file contains hidden or 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
void main() { | |
var testString = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit,'; | |
var resLengh = 17; | |
print(middleCut(testString, resLengh)); | |
} | |
String middleCut(String text, int length){ | |
const numberOfDots = 3; | |
final dotsString = List<String>.filled(numberOfDots, '.').join(); | |
int leftSizeLengh = ((length - numberOfDots) / 2).floor(); | |
int rightSizeLength = text.length - leftSizeLengh; | |
return '${text.substring(0, leftSizeLengh)}$dotsString${text.substring(rightSizeLength)}'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment