Last active
July 11, 2021 10:22
-
-
Save filiph/d4e0c0a9efb0f869f984317372f5bee8 to your computer and use it in GitHub Desktop.
A functional way to capitalize each word in a sentence (a.k.a. Title Case). This is not efficient -- use something like string_scanner if you need to run this in a tight loop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main() { | |
var city = "new york"; | |
print(titleCase(city)); | |
} | |
/// Inefficient way of capitalizing each word in a string. | |
String titleCase(String text) { | |
if (text.length <= 1) return text.toUpperCase(); | |
var words = text.split(' '); | |
var capitalized = words.map((word) { | |
var first = word.substring(0, 1).toUpperCase(); | |
var rest = word.substring(1); | |
return '$first$rest'; | |
}); | |
return capitalized.join(' '); | |
} |
main() {
String spanish = "esto es una prueba con varias palabras";
/// First option.
print(titleCase(spanish));
/// Second option.
String titleCaseVar = spanish
.split(' ')
.map((word) => word[0].toUpperCase() + word.substring(1))
.join(' ');
print(titleCaseVar);
}
/// My way of capitalizing each word in a string.
String titleCase(String text) {
if (text == null) throw ArgumentError("string: $text");
if (text.isEmpty) return text;
/// If you are careful you could use only this part of the code as shown in the second option.
return text
.split(' ')
.map((word) => word[0].toUpperCase() + word.substring(1))
.join(' ');
}
I've converted to String extension as below:
extension CapExtension on String {
//first letter only
String get inCaps => '${this[0].toUpperCase()}${this.substring(1)}';
//all letter in string
String get allInCaps => this.toUpperCase();
//first letter for each word in a string
String get titleCase => this
.split(' ')
.map((word) => word[0].toUpperCase() + word.substring(1))
.join(' ');
}
Hope it helps. :)
Here's my implementation, using a StringBuffer
and UTF-16 values (this capitalizes letters after periods as well):
/// Fixes name cases; Capitalizes Each Word.
String normaliseName(String name) {
final stringBuffer = StringBuffer();
var capitalizeNext = true;
for (final letter in name.toLowerCase().codeUnits) {
// UTF-16: A-Z => 65-90, a-z => 97-122.
if (capitalizeNext && letter >= 97 && letter <= 122) {
stringBuffer.writeCharCode(letter - 32);
capitalizeNext = false;
} else {
// UTF-16: 32 == space, 46 == period
if (letter == 32 || letter == 46) capitalizeNext = true;
stringBuffer.writeCharCode(letter);
}
}
return stringBuffer.toString();
}
Here's my implementation, using a
StringBuffer
and UTF-16 values (this capitalizes letters after periods as well):/// Fixes name cases; Capitalizes Each Word. String normaliseName(String name) { final stringBuffer = StringBuffer(); var capitalizeNext = true; for (final letter in name.toLowerCase().codeUnits) { // UTF-16: A-Z => 65-90, a-z => 97-122. if (capitalizeNext && letter >= 97 && letter <= 122) { stringBuffer.writeCharCode(letter - 32); capitalizeNext = false; } else { // UTF-16: 32 == space, 46 == period if (letter == 32 || letter == 46) capitalizeNext = true; stringBuffer.writeCharCode(letter); } } return stringBuffer.toString(); }
Thank you so much! @hacker1024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OK, what is an efficient way?