capitalize method

String capitalize()

Returns a copy of this string having its first letter uppercased, or the original string, if it's empty or already starts with an upper case letter.

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);
  }
}