DEV Community

loading...

Discussion on: Daily Challenge #190 - capitalizeFirstLast

Collapse
exts profile image
Lamonte

Ugly, but it works (dart)

String capitalizeFirstLast(String words) {
  return words.split(" ").map((word) {
    word = word.toLowerCase();
    word = word[0].toUpperCase() + word.substring(1, word.length);
    if(word.length > 1) {
      word = word.substring(0, word.length-1) + word[word.length-1].toUpperCase();
    }
    return word;
  }).toList().join(" ");
}