initials property
String
get
initials
Returns the first two letters.
Example:
final text = 'John Doe';
final initials = text.initials;
// returns: 'JD'
Implementation
String get initials {
// check if the string is empty or just whitespace
if (trim().isEmpty) return '-';
try {
return split(' ')
.map((part) => part.withoutSymbols)
.where((part) => part.isNotEmpty)
.map((part) => part[0])
.take(2)
.join()
.toUpperCase();
} catch (_) {
return '-';
}
}