toPascalCase method
Converts the string to PascalCase.
If the string is empty, it returns the original string.
The conversion is done by splitting the string into words using whitespace, underscore, and hyphen as delimiters. Each word is then capitalized by making the first letter uppercase and the remaining letters lowercase. The words are then joined together to form the PascalCase string.
Example:
final input = 'hello_world';
final output = input.toPascalCase(); // Output: 'HelloWorld'
Implementation
String toPascalCase() {
if (isEmpty) {
return this;
}
final words = trim().split(RegExp(r'\s+|_+|-+'));
final pascalCaseWords = words.map((word) {
final firstLetter = word.substring(0, 1).toUpperCase();
final remainingLetters = word.substring(1).toLowerCase();
return '$firstLetter$remainingLetters';
});
return pascalCaseWords.join();
}