getInitials static method
Implementation
static String getInitials(String name) {
// replace all non-alphabetic characters
name = name.replaceAll(RegExp(r'[^a-zA-Z\s]'), '');
final List<String> parts = name.split(' ');
if (parts.isEmpty) {
// get the first 2 characters (title cased)
String first = name.substring(0, 1).toUpperCase();
if (name.length > 1) {
String second = name.substring(1, 2).toUpperCase();
return first + second;
}
return first;
}
// get the first two characters
String first = parts[0].substring(0, 1).toUpperCase();
if (parts.length > 1) {
String second = parts[1].substring(0, 1).toUpperCase();
return first + second;
}
// append with the 2nd character of the first part
if (parts[0].length > 1) {
String second = parts[0].substring(1, 2).toUpperCase();
return first + second;
}
return first;
}