initials property
String
get
initials
!~~~~~~~~~~~~~~~~~~~~~~~ Initials ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Returns the initials of the words in the string, up to a maximum of three.
This method splits the string by spaces and extracts the first letter of each word, converting it to uppercase. It returns a string containing the initials. Only the first three words are considered.
Example:
"John Doe".initials; // Returns "JD"
"Jane Mary Doe".initials; // Returns "JMD"
"A B C D".initials; // Returns "ABC"
" John Doe".initials; // Returns "JD" (handles leading spaces)
"".initials; // Returns "" (handles empty string)
"John".initials; // Returns "J"
Returns: A String containing the initials.
Implementation
String get initials {
List<String> words = split(' ');
String initials = '';
for (int i = 0; i < words.length; i++) {
final word = words[i];
if (i < 3) {
initials += (word.isNotEmpty ? word[0] : '').toUpperCase();
}
}
return initials;
}