Skip to content

Instantly share code, notes, and snippets.

@smmcdonald
Created November 25, 2019 15:34
Show Gist options
  • Select an option

  • Save smmcdonald/65ed5cdca3120c02e2b04735564cbf75 to your computer and use it in GitHub Desktop.

Select an option

Save smmcdonald/65ed5cdca3120c02e2b04735564cbf75 to your computer and use it in GitHub Desktop.
Utility for space delimited string capitalization in dart because I couldn't find a public one.
/// Returns a string with capitalized first character of every empty space seperated word.
///
/// Example:
/// print(capitalize("123 Sean is super AwEsOmE 456!"));
/// => 123 Sean Is Super Awesome 456!
String capitalize(String string) {
if (string == null) {
throw ArgumentError("string: $string");
}
if (string.isEmpty) {
return string;
}
//Lowercase so that we have something to capitalize
var words = string.toLowerCase().split(" ");
var returnString = "";
for (var word in words) {
returnString += word[0].toUpperCase() + word.substring(1);
if (word != words.last) {
returnString += " ";
}
}
return returnString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment