elideTo method

String elideTo(
  1. int limit
)

Truncates this string to limit characters, replacing the middle with '...'.

If this string's length exceeds limit, the middle is replaced with '...' to maintain approximately equal head and tail visibility.

Examples:

  • "hello".elideTo(10) → "hello" (unchanged, under limit)
  • "hello world test".elideTo(12) → "hello...test"

Implementation

String elideTo(int limit) {
  if (length > limit) {
    final headLength = limit ~/ 2 - 1;
    final tailLength = limit - headLength - 3;
    return '${substring(0, headLength)}...${substring(length - tailLength)}';
  }
  return this;
}