capitalize method

String capitalize()

Returns a copy of this string having its first letter uppercased and the rest lowercased.

void foo() {
  print('abcd'.capitalize()); // 'Abcd'
  print(''.capitalize());     // ''
  print('Abcd'.capitalize()); // 'Abcd'
  print('ABCD'.capitalize()); // 'Abcd'
}

Implementation

String capitalize() {
  switch (length) {
    case 0:
      return this;
    case 1:
      return toUpperCase();
    default:
      return substring(0, 1).toUpperCase() + substring(1).toLowerCase();
  }
}