Skip to content

Instantly share code, notes, and snippets.

@kherel
Last active April 22, 2020 06:21
Show Gist options
  • Save kherel/aa6fb66e99149f9f8858f03e88b311c3 to your computer and use it in GitHub Desktop.
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
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