Home > Back-end >  How to check if a letter is upper/lower cased?- Flutter
How to check if a letter is upper/lower cased?- Flutter

Time:11-05

The question is pretty self-explainable. I want to check if certain letter is uppercase or another letter is lowercase. Could you give me any examples of how to do that in Flutter/Dart?

CodePudding user response:

you can use the .toUpperCase() in a boolean statement:

bool isUppercased(String str){
  return str == str.toUpperCase();
}

CodePudding user response:

The one solution that is coming to my mind is to check its ASCII code. The ASCII code of a-z starts at 97 and ends at 122. Similarly, in the case of Uppercase letters A-Z it starts from 65 and ends at 90.

Keeping this in mind you can use the method string.codeUnitAt(index) which will return you the ASCII code and later you can check its range and find its an Uppercase or lowercase.

Have a look into this example

main() {

 String ch = 'Rose';
 print(' ASCII value of ${ch[0]} is ${ch.codeUnitAt(0)}');
   print(' ASCII value of ${ch[1]} is ${ch.codeUnitAt(1)}');

}

The output will be:

 ASCII value of R is 82

 ASCII value of o is 111

Now you can compare with the range using if statement and find out.

CodePudding user response:

If you want to use regular expressions, here is how you could do:

bool isUpperCase(String letter) {
  assert(s.length == 1);
  final regExp = RegExp('[A-Z]');
  return regExp.hasMatch(letter);
}
  • Related