toPascalCase function
Converts a string input
to pascal case format.
If the input
is null or empty, an empty string is returned.
The function trims leading and trailing whitespace from the input
and splits it into an array of words using underscores, spaces, and hyphens
as delimiters. Each word (including the first word) is capitalized
and appended to the result string. The remaining characters in each word
are converted to lowercase.
Example: toPascalCase('hello_world') => 'HelloWorld' toPascalCase('hello-world') => 'HelloWorld' toPascalCase('hello world') => 'HelloWorld' toPascalCase('HelloWorld') => 'HelloWorld'
Returns the converted string in pascal case format.
Implementation
String toPascalCase(String? input) {
if (input == null || input.isEmpty) return '';
// Split the string at spaces, underscores, hyphens, or camelCase boundaries.
final words = input.trim().split(RegExp(r'[_\s-]+|(?<=[a-z])(?=[A-Z])'));
var result = '';
for (final word in words) {
result += word[0].toUpperCase() + word.substring(1).toLowerCase();
}
return result;
}