티스토리 뷰

1. 대소문자 변환

문자열(String)인 경우

  • 대문자로 변환: toUpperCaese()
  • 소문자로 변환 : toLowerCase()

예시

String str = "abc"

str = str.toUpperCase(); //"ABC"

str = str.toLowerCase(); //"abc"

 
문자(Char)인 경우

  • 대문자로 변환 : Character.toUpperCase(변환을 원하는 단어)
  • 소문자로 변환: Character.toLowerCase(변환을 원하는 단어)

예시

Char c = "a"

c = Character.toUpperCase(c); //"A"

c = Character.toLowerCase(c); //"a"

 

2. 대소문자 확인

문자열(String)인 경우 - 대문자 또는 소문자로 변경 후 비교

private static boolean isStringUpperCase(String str){
  if(!str.equals(str.toUpperCase())) return false;
  return true;
}

public static void main(String args[]) {
  String strValue1 = "ABC";
  String strValue2 = "abc";

  System.out.println("\"" + strValue1 + "\"는 대문자인가? " + isStringUpperCase(strValue1));
  System.out.println("\"" + strValue2 + "\"는 대문자인가? " + isStringUpperCase(strValue2));
}

 
문자(Char)인 경우

  • Character.isUpperCase(문자)
  • Character.isLowerCase(문자)
public static void main(String args[]) {
  char charValue = 'A';

  if(Character.isUpperCase(charValue)) {
    System.out.println("A는 대문자입니다.");
  }

  if(Character.isLowerCase(charValue)) {
    System.out.println("A는 소문자입니다.");
  }
}