Jake
Jake

Reputation: 2056

Flutter - String check for ellipsis

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.

Upvotes: 1

Views: 1258

Answers (2)

TruongSinh
TruongSinh

Reputation: 4866

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}');
}

Upvotes: 1

greyaurora
greyaurora

Reputation: 907

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.

Upvotes: 1

Related Questions