📜  dart truncate - Dart (1)

📅  最后修改于: 2023-12-03 15:14:36.182000             🧑  作者: Mango

Dart Truncate

Dart Truncate is a package in Dart that provides utility functions to truncate strings. Truncation is the process of shortening a string by removing parts of it. This is useful in situations where you need to display or use only a portion of a string.

Features

Dart Truncate provides the following features:

  • Truncation based on a specific number of characters
  • Truncation based on a specific number of words
  • Truncation with a custom ellipsis
  • Truncation with the option to preserve HTML tags
Usage
Installing

To use Dart Truncate, add it to the dependencies section of your pubspec.yaml file:

dependencies:
  truncate: ^4.0.0
Importing

Then, import truncate.dart as follows:

import 'package:truncate/truncate.dart';
Truncating by Characters

To truncate a string by a specific number of characters, use the truncate function:

String result = truncate('Dart Truncate is awesome!', 10);

This will return the string "Dart Trunc..." - the first 10 characters with an ellipsis at the end.

Truncating by Words

To truncate a string by a specific number of words, use the truncateWords function:

String result = truncateWords('Dart Truncate is awesome!', 2);

This will return the string "Dart Truncate..." - the first two words with an ellipsis at the end.

Customizing the Ellipsis

To customize the ellipsis used in the truncated string, use the ellipsis parameter:

String result = truncate('Dart Truncate is awesome!', 10, ellipsis: '***');

This will return the string "Dart Trunc***" - the first 10 characters with "***" at the end.

Preserving HTML Tags

To preserve HTML tags when truncating a string, use the preserveTags parameter:

String result = truncate('Dart <b>Truncate</b> is awesome!', 10, preserveTags: true);

This will return the string "Dart Trunc..." - the first 10 characters with the HTML tag intact.

Conclusion

Dart Truncate is a useful Dart package for truncating strings. With its flexible options for truncating by characters or words, customizing the ellipsis, and preserving HTML tags, it's a valuable tool for any Dart programmer.