Skip to content

Instantly share code, notes, and snippets.

@JEuler
Created June 5, 2022 18:09
Show Gist options
  • Save JEuler/77b508b8c8317415730adefc2e888a0c to your computer and use it in GitHub Desktop.
Save JEuler/77b508b8c8317415730adefc2e888a0c to your computer and use it in GitHub Desktop.
class StringHelper {
static String removeAllHtmlTags(String htmlText) {
final RegExp exp = RegExp("<[^>]*>", multiLine: true);
return htmlText.replaceAll(exp, '');
}
static String shrinkSecondName(String? name) {
if (name != null && name.isNotEmpty) {
final nameStr = name.split(' ');
return nameStr[0] +
(nameStr.length > 1
? (' ${nameStr[1].isNotEmpty ? nameStr[1][0].capitalize() : ''}.')
: '');
} else {
return '';
}
}
static String getInitials(String? nameString) {
if (nameString == null) return ' ';
if (nameString.trim().isEmpty) return ' ';
final nameArray = nameString
.trim()
.replaceAll(RegExp(r'\s+\b|\b\s'), ' ')
.split(' ')
.take(2)
.toList();
// ignore: prefer_if_null_operators
final initials = ((nameArray[0])[0]) +
(nameArray.length == 1
? ''
: ((nameArray[nameArray.length - 1]).isNotEmpty
? (nameArray[nameArray.length - 1])[0]
: ''));
return (initials.toUpperCase()).capitalize();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment