valerybodak
valerybodak

Reputation: 4413

Checking if the first letter of string is in uppercase in Dart

I want to check if the first letter of string is in uppercase in Dart language. How can I implement it? Thanks in advance.

Upvotes: 6

Views: 10111

Answers (5)

Mahesh Jamdade
Mahesh Jamdade

Reputation: 20399

Verify the ascii code of the character using codeUnites

Add this extension

extension Case on String{
    // isuppercase
    bool isUpperCase(){
        int ascii = codeUnitAt(0);
        return ascii >= 65 && ascii <= 90;
    }
    // islowercase
    bool isLowerCase(){
        int ascii = codeUnitAt(0);
        return ascii >= 97 && ascii <= 122;
    }
}

use it like this

String letter = 'A';
print(letter.isUpperCase()); // true

Upvotes: 1

Haroun Hajem
Haroun Hajem

Reputation: 5638

You can use the validators library if you are not already using it. Then use this method

isUppercase(String str) → bool check if the string str is uppercase

Don't forget to import the dependency, see documentation, to the pubspec.yaml and to your code import 'package:validators/validators.dart';.

Example code:

if(isUppercase(value[0])){
  ... do some magic
}

You should check that the value is not empty and not null first for safety. Like this:

if(value != null && value.isNotEmpty && isUppercase(value[0])){
  ... do amazing things
}

Upvotes: 2

Noorus Khan
Noorus Khan

Reputation: 1476

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

This seems to be elegant way for checking uppercase

String s='Hello';

bool isUpper = isUppercase(s[0]);
print(isUpper);  //true

isUpper = isUppercase(s[1]);
print(isUpper);  //false

Upvotes: 0

danypata
danypata

Reputation: 10175

The simplest way I can think of is to compare the first letter of the string with the uppercase equivalent of it. Something like:

bool isUpperCase(String string) {
    if (string == null) {
      return false;
    }
    if (string.isEmpty) {
      return false;
    }
    if (string.trimLeft().isEmpty) {
      return false;
    }
    String firstLetter = string.trimLeft().substring(0, 1);
    if (double.tryParse(firstLetter) != null) {
      return false;
    }
    return firstLetter.toUpperCase() == string.substring(0, 1);      
}

Updated the answer to take in consideration digits.

Also @Saed Nabil is right, this solution will return true if the string starts with any character that is not a letter (except for digits).

Upvotes: 12

Saed Nabil
Saed Nabil

Reputation: 6871

check this code it will return the actual uppercase letter else will return null

void main(){
  var myString = "1s you said";
  var firstCapital = firstCapitalLetter(myString);

  if( firstCapital != null){
    print("First Capital Letter is ${firstCapital}");
  }else{
    print("Not found");
  }
}
String firstCapitalLetter(String myString){

  final allCapitals = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //string.substring(0, 1).toUpperCase() == string.substring(0, 1) will not work with for ex. numbers;

   if (myString == null) {
      return null;
    }
    if (myString.isEmpty) {
      return null;
    }
    if (myString.trimLeft().isEmpty) {
      return null;
    }

  if( allCapitals.contains(myString[0])){
       return myString[0];
  }else{
       return null;
  }
 }

this is a typical case for Optional type of Java language , please check this library if you prefer functional style code optional package

Upvotes: 0

Related Questions