A method to detect if string contains only uppercase letter in Java

My approach

This method loop through each character in the string, and determine if each character is in upper case. Upper case letter value is from 97 to 122.

public static boolean testAllUpperCase(String str){
		for(int i=0; i<str.length(); i++){
			char c = str.charAt(i);
			if(c >= 97 && c <= 122) {
				return false;
			}
		}
		//str.charAt(index)
		return true;
	}

Any other better approach regarding the performance?

Option 1

From Hans Dampf's comment below:

Use java.lang.Character#isUpperCase() and #isLetter() instead of the magic numbers.

Category >> Basics  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>

  1. Ruzo on 2013-9-19

    from a string s1 create a copy s2, move to upper case s2 with toUpperCase method, so compare with s1.equals(s2)

  2. ryanlr on 2013-9-19

    Thanks. But I think the performance of your method is worse. The equals() method does the following:

    if (n == anotherString.count) {
    char v1[] = value;
    char v2[] = anotherString.value;
    int i = offset;
    int j = anotherString.offset;
    while (n– != 0) {
    if (v1[i++] != v2[j++])
    return false;
    }
    return true;
    }

  3. Anthony Kaser on 2013-9-20

    Your example will return false if the String contains any lower case characters. It won’t return false if there are any non-alpha chars like punctuations or numbers.

    for example
    testAllUpperCase(“ABC123-#!”) will return true. None of the chars are lower case.

    If your objective is to test a string to ensure it contains ONLY upper case chars then:

    Upper case characters are actually between 65 (‘A’) and 90 (‘Z’)

    I would modify the ‘if’ statement to read
    if ( c 90) return false;
    OR
    if(c ‘Z’) return false;
    Both are functionally equivalent, but I prefer the latter as it is easier to read.

    I always view the >= and <= operators as two step operators.
    so for (c<= 'Z') you get
    1) is c less than ( ‘Z’ as this would be outside the range.

  4. Hans Dampf on 2013-9-23

    I’d prefer to use java.lang.Character#isUpperCase() and #isLetter() instead of the magic numbers.

  5. Axel Howind on 2013-9-24

    That’s just bad. What about languages using non-ASCII characters like german, french, and all the others?

  6. moisellegyg on 2014-4-4

    What about using Character.getType(ch) == Character.UPPERCASE_LETTER ?

  7. dfme on 2015-1-7

    How about using Regex? It might not outperform your solution but it is easier to understand and less verbose…

    Pattern p = Pattern.compile(“[A-Z]*”);
    System.out.println(p.matcher(“ABCD”).matches()); // true
    System.out.println(p.matcher(“ABcD”).matches()); // false
    System.out.println(p.matcher(“ABC123-#!.”).matches()); // false

  8. Elif on 2015-1-27

    String a = “eLif”;

    String b = a.toLowerCase();

    if(a.equals(b))

    System.out.println(“there is no uppercase letter in it”);

    else

    System.out.println(“there is a uppercase letter in it”);

  9. Danon on 2015-2-19

    s.toUpperCase().equals(s);

  10. samrudhi on 2016-1-6

    Hi, there is an error in your writeup it should be Lower case letter value is from 97 to 122

  11. Andrey on 2016-4-7

    Yeah, I’ve noticed that too. But the code itself is fine, an author made an inversion of lowercase letters, so now method works fine even if there are whitespaces, punctuation or Greek alphabet 🙂

  12. gexiu on 2016-6-28

    lower case letter value is from 97 to 122. not upper case

  13. zhenkai hou on 2016-11-23

    what about org.apache.commons.lang3.StringUtils.isAllUpperCase() ?

    return StringUtils.isAllUpperCase(String str);

Leave a comment

*