๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ์ƒํ™œ/Java

[JAVA] char ์ •๋ฆฌ

by cocococo331 2022. 3. 1.

ํ•˜๋‚˜์”ฉ ์ถ”๊ฐ€ ์ •๋ฆฌํ•ด๋‚˜๊ฐˆ ์˜ˆ์ •

 

1. charAt(idx) 

  • ๋ฌธ์ž์—ด์˜ index๋ฒˆ์งธ ์œ„์น˜์— ์žˆ๋Š” character์„ return
      String str = "Hello World";
      char c = str.charAt(0);
      System.out.println("c==?"+c); //H

      //์‹ค์ œ ๊ตฌํ˜„๋ถ€
      //    public char charAt(int index) {
      //        if ((index < 0) || (index >= value.length)) {
      //            throw new StringIndexOutOfBoundsException(index);
      //        }
      //        return value[index];
      //    }

 

2. String.toCharArray()

  • ๋ฌธ์ž์—ด์„ charArray๋กœ ๋ณ€ํ™˜
      String str1 = "Hello World";
      char[] str1Arr = str1.toCharArray();

      for(char c1 : str1Arr) {
         System.out.println(c1);
      }

      //์‹ค์ œ ๊ตฌํ˜„๋ถ€
//    public char[] toCharArray() {
//       char result[] = new char[value.length];
//       System.arraycopy(value, 0, result, 0, value.length);
//       return result;
//    }

 

3. char ํƒ€์ž…์˜ ๋Œ€์†Œ๋ฌธ์ž ๋ณ€ํ™˜

 

Character.toUpperCase(char) / Character.toLowerCase(char)

  • char์„ ๋Œ€๋ฌธ์ž char๋กœ ๋ณ€ํ™˜, ์†Œ๋ฌธ์ž char๋กœ ๋ณ€ํ™˜
  • ์บ๋ฆญํ„ฐ ๋ฟ ์•„๋‹ˆ๋ผ ์œ ๋‹ˆ์ฝ”๋“œ๋กœ ์ž…๋ ฅ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.
  • 65~90 : ๋Œ€๋ฌธ์ž / 97 ~ 122 : ์†Œ๋ฌธ์ž = ์†Œ๋ฌธ์ž - 32 = ๋Œ€๋ฌธ์ž
  • char๋Š” ์ •์ˆ˜ํ˜•์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ”๋กœ ์•„์Šคํ‚ค ์ฝ”๋“œ๋กœ ๋น„๊ต๋จ.
    • (char)(char+32) / (char)(char-32)
      char c2 = 'a'; //a 97 A 65 (char Unicode)
      char c3 = 'A'; //a 97 A 65 (char Unicode)
      char upperC2 = Character.toUpperCase(c2);
      char lowerC3 = Character.toLowerCase(c3);
      System.out.println("upper="+upperC2+" / lower="+lowerC3);

      char upperC2UniCode = (char) Character.toUpperCase(65);
      char lowerC3UniCode = (char) Character.toLowerCase(97);
      System.out.println("upper="+upperC2UniCode+" / lower="+lowerC3UniCode);

 

4.  char ํƒ€์ž…์˜ ๋Œ€์†Œ๋ฌธ์ž ์ฒดํฌ

  1. java.lang.Character
    1. Character.isLowerCase(c)
    2. Character.isUpperCase(c)
  2. Ascii Code 
    1. if(c >= 97 && c< 122) //์†Œ๋ฌธ์ž
    2. if(c >= 65 && c <= 90) //๋Œ€๋ฌธ์ž