toPascalCase method
Returns a String converted to PascalCase.
The optional parameter addSpace determines whether to include spaces
between the words in the resulting String. By default, it is set to false.
If set to true, the resulting String will have spaces between the words.
The conversion is done by splitting the original String by any whitespace or underscore characters and then capitalizing the first letter of each resulting word while converting the rest of the letters to lowercase.
Example:
final String input = 'foo bar_baz';
final String output = input.toPascalCase(addSpace: true);
print(output); // Output: 'Foo BarBaz'
Implementation
String toPascalCase({bool addSpace = false}) {
List<String> words = this.split(new RegExp(r'[\s_]+'));
List<String> pascalWords = [];
for (String word in words) {
String pascalWord = '${word[0].toUpperCase()}${word.substring(1).toLowerCase()}';
pascalWords.add(pascalWord);
}
String result = pascalWords.join(addSpace ? ' ' : '');
return result;
}