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.
<pre><code> String foo = "bar"; </code></pre>
-
Jasur Ahmadov
-
zhenkai hou
-
gexiu
-
Andrey
-
samrudhi
-
Danon
-
Elif
-
dfme
-
moisellegyg
-
Axel Howind
-
Hans Dampf
-
Anthony Kaser
-
ryanlr
-
Ruzo