How to check if the first character of a string is uppercase in Dart

2 Answers

0 votes
void main() {
    var str = "Dart";

    if (str[0].toUpperCase() == str[0]) {
        print("The first character is uppercase");
    } else {
        print("The first character is not uppercase");
    }
}




/*
run:

The first character is uppercase

*/

 



answered Nov 4, 2022 by avibootz
0 votes
bool isFirstCharacterUpperCase(String str) {
    if (str == null || str.isEmpty || str.trimLeft().isEmpty) {
        return false;
    }

    String firstChar = str.trimLeft().substring(0, 1);
    if (double.tryParse(firstChar) != null) {
        return false;
    }
    
    return firstChar.toUpperCase() == str.substring(0, 1);      
}

void main() {
    var str = "Dart";

    if (isFirstCharacterUpperCase(str)) {
        print("The first character is uppercase");
    } else {
        print("The first character is not uppercase");
    }
}





/*
run:

The first character is uppercase

*/

 



answered Nov 4, 2022 by avibootz

Related questions

1 answer 147 views
1 answer 128 views
1 answer 194 views
1 answer 149 views
1 answer 155 views
155 views asked Oct 12, 2022 by avibootz
...