Flutter - String check for ellipsis

661

Solution 1

String input = 'your string';

if (!input.endsWith('...')) {
  input += '...';
}

Key here is String.endsWith(), it's the easiest way to know if the content already ends with an ellipsis.

Solution 2

Enhanced answered from @greyaurora

  • be careful trailing space, e.g. my data...
  • be careful with different ways to display ellipsis, e.g. ... (3 dots ) vs (1-char ellipsis)
void main() {
  String input = 'your string';
  String trimmed = input.trim();

  if (!trimmed.endsWith('...') && !trimmed.endsWith('…')) {
    trimmed += '…';
  }
  print('hello ${trimmed}');
}
Share:
661
Jake
Author by

Jake

Work email: jake(at)squaredsoftware.co.uk Please support me by downloading my new iOS application, travelrecce!

Updated on December 09, 2022

Comments

  • Jake
    Jake over 1 year

    My flutter apps displays description strings which are fetched from a third party, thus these descriptions may already be fitted with ellipsis as to lead the story onwards but the api only fetches around 7 lines or so of the description.

    I want every description to contain an ellipsis but don't want to add ellipsis to strings that already contain them.

    I want to turn this

    scan the face of each account holder as

    into this

    scan the face of each account holder as...

    but if the original already contains this ellipsis, it should be skipped.