Ashta
Ashta

Reputation: 191

how to uppercase each letter in dart?

I tried this code but the result is just uppercase first letter (not each letter)

String ucwords(String input) {
    if (input == null) {
      throw new ArgumentError("string: $input");
    }
    if (input.length == 0) {
      return input;
    }
    return input[0].toUpperCase() + input.substring(1);
}

Upvotes: 2

Views: 6089

Answers (2)

Marco Domingos
Marco Domingos

Reputation: 715

Well, i didn't understand you question completely, but if you want to put each letter in Uppercase, this means all in Uppercase, well you can just use .toUpperCase() like this:

//This will print 'LIKE THIS'
print('like this'.toUpperCase());

But if you want to put an specific letter in UpperCase, you can just use the Text_Tools package:

https://pub.dev/packages/text_tools

Example

//This will put the letter in position 2 in UpperCase, will print 'liKe this'
print(TextTools.toUppercaseAnyLetter(text: 'like this', position: 2));

If this be of any help

Upvotes: 0

Matt
Matt

Reputation: 3190

I suggest you read the documentation for toUpperCase(). It gives an example of exactly what you want to do.

Your code should read:

return input.toUpperCase();

Upvotes: 5

Related Questions