P-J
P-J

Reputation: 219

First char to upperCase rest to lower

I am using this string formatter to have first char toUpperCase in given string (user name), however, if user name is whole in capital letters than it's obviousy gives me nothing. Do you have any idea how to refactor it to make whole string toLowerCase and then make first one capital?

static String upperFirstCase(String str) {
    return "${str[0].toUpperCase()}${str.substring(1)}";
  }

Upvotes: 0

Views: 112

Answers (2)

Ashok
Ashok

Reputation: 3611

Approach 1

String capitalize(String str) => (str != null && str.length > 1)
    ? str[0].toUpperCase() + str.substring(1)
    : str != null ? str.toUpperCase() : null;

Approach 2

String str= 'sample';      
print(${str[0].toUpperCase()}${str.substring(1).toLowerCase()});

Upvotes: 1

ajay
ajay

Reputation: 1000

try this

String upperFirstCase(String s) =>(s[0].toUpperCase() + s.substring(1).toLowerCase);

Upvotes: 2

Related Questions