Created
November 25, 2019 15:34
-
-
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.
This file contains hidden or 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
| /// 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