文字列内の文字の大文字と小文字の判断
実現方法一
ASC‖コード値
実現方法2
java.lang.Character
ASC‖コード値
- public class TestStringCase {
- public static void main(String[] args) {
- String s = "ABAabb$&^$#BAAb898B#@%aa";
- int cU = 0, cL = 0, cO = 0;
- for(int i=0; i<s.length(); i++) {
- char c = s.charAt(i);
-
- if(c >= 'A' && c <= 'Z') {
- cU ++;
- } else if( c >= 'a' && c <='z') {
- cL ++;
- } else {
- cO ++;
- }
- }
-
- System.out.println(" :" + cU);
- System.out.println(" :" + cL);
- System.out.println(" :" + cO);
- }
- }
実現方法2
java.lang.Character
- public class TestStringCase {
- public static void main(String[] args) {
- String s = "ABAabb$&^$#BAAb898B#@%aa";
- lCount=0;
- uCount=0;
- oCount=0;
- for (int i= 0;i<s.length();i++){
- char c = s.charAt(i);
- if(Character.isLowerCase(c)){
- lCount++;
- }else if (Character.isUpperCase(c)){
- uCount++;
- }
- else{
- oCount++;
- }
- }
- System.out.println(lCount+""+uCount+""+oCount);
- }
java.lang.String
- public class TestString{
- public static void main(String[] args) {
- String s = "ABAabb$&^$#BAAb898B#@%aa";
- String sL="abcdefghijklmnopqrstuvwxyz";
- String sU="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- lCount=0;
- uCount=0;
- oCount=0;
- for (i=0;i<s.length();i++){
- char c = s.charAt(i);
- if(sL.indexOf(c)!=-1){
- lCount++;
- }else if(sL.indexOf(c)!=-1){
- uCount++;
- }else{
- oCount++;
- }
- }
- System.out.println(" :" + uCount);
- System.out.println(" :" + lCount);
- System.out.println(" :" + oCount);
- }
- }