Java/문자열

[Java]특정 위치의 문자를 유니코드로 가져오는 방법

DevStory 2022. 8. 17.

특정 위치의 문자를 유니코드로 가져오는 방법

Java의 String 클래스는 문자열에서 특정 위치의 문자를 유니코드로 반환하는 몇 가지 메서드를 제공합니다.

 

codePointAt(int index)

- 특정 위치의 문자를 유니코드로 가져옵니다.

 

codePointBefore(int index)

- 특정 위치의 앞에 존재하는 문자를 유니코드로 가져옵니다.

 

codePointCount(int beginIndex, int endIndex)

- 문자열의 특정 범위에 존재하는 유니코드 개수를 반환합니다.

 

메서드 이름에 공통적으로 "codePoint"라는 단어가 포함되어있는데, "codePoint"는 유효한 유니코드 값을 의미합니다.

 

이번 포스팅은 위에서 언급한 세 가지 메서드 사용 방법을 소개합니다.


codePointAt 메서드

codePointAt() 메서드는 charAt() 메서드와 유사합니다.

 

charAt() 메서드는 특정 위치의 문자를 반환하며, codePointAt() 메서드는 특정 위치의 문자를 10진수 유니코드로 반환합니다.

 

다음 예제는 문자열에서 첫 번째 위치의 문자와 마지막 위치의 문자를 10진수 유니코드로 반환합니다.

public static void main(String args[]) {
  String strValue = "Hello Java";

  int firstCodePoint = strValue.charAt(0);
  int lastCodePoint = strValue.codePointAt(strValue.length() - 1);

  System.out.println("firstCodePoint: " + firstCodePoint);
  System.out.println("lastCodePoint: " + lastCodePoint);
}

[실행 결과]

firstCodePoint: 72
lastCodePoint: 97

유니코드는 영어, 숫자, 특수 문자뿐만 아니라 한글, 중국어, 일본어 등 다양한 언어를 표현할 수 있습니다.

 

다음 예제는 한글로 구성된 문자열에서 첫 번째 위치의 문자와 마지막 위치의 문자를 10진수 유니코드로 반환합니다.

public static void main(String args[]) {
  String strValue = "안녕 자바";

  int firstCodePoint = strValue.charAt(0);
  int lastCodePoint = strValue.codePointAt(strValue.length() - 1);

  System.out.println("firstCodePoint: " + firstCodePoint);
  System.out.println("lastCodePoint: " + lastCodePoint);
}

[실행 결과]

firstCodePoint: 50504
lastCodePoint: 48148

만약, codePointAt() 메서드에 문자열 인덱스 범위를 벗어나는 값을 전달하면 StringIndexOutOfBoundsException이 발생합니다.

public static void main(String args[]) {
  String strValue = "Hello Java"; // 인덱스 0~9까지 접근 가능
  int CodePoint = strValue.codePointAt(15);

  System.out.println("CodePoint: " + CodePoint);
}

[에러 내용]


codePointBefore 메서드

codePointBefore() 메서드는 특정 위치 앞에 있는 문자를 10진수 유니코드로 반환합니다.

 

다음 예제는 codePointAt() 메서드에 0을 전달한 결과와 codePointBefore() 메서드에 1을 전달한 결과가 동일한 것을 확인할 수 있습니다.

public static void main(String args[]) {
  String strValue = "Hello Java";
  int firstCodePoint = strValue.codePointAt(0);
  int beforeCodePoint = strValue.codePointBefore(1);

  System.out.println("firstCodePoint: " + firstCodePoint);
  System.out.println("beforeCodePoint: " + beforeCodePoint);
}

[실행 결과]

firstCodePoint: 72
beforeCodePoint: 72

codePointAt() 메서드와 마찬가지로 문자열 인덱스 범위를 벗어나는 값을 전달하면 StringIndexOutOfBoundsException이 발생합니다.


codePointCount 메서드

codePointCount() 메서드는 문자열 특정 범위에서 유효한 유니코드 값의 개수를 반환합니다.

 

codePointCount() 메서드는 시작 인덱스는 범위에 포함되지만, 종료 인덱스는 범위에 포함되지 않습니다.

 

첫 번째 매개변수(startIndex)에 0을 전달하고 두 번째 매개변수(endIndex)에 10을 전달하면, 0 <= index < 10 범위의 유효한 유니코드 개수를 반환합니다. 

strValue.codePointCount(0, 10);

다음 예제는 문자열의 첫 번째 위치에서 문자열 마지막 위치까지 유효한 유니코드 개수를 가져옵니다.

public static void main(String args[]) {
  String strValue = "Hello Java";
  int codePointCount = strValue.codePointCount(0, strValue.length());

  System.out.println("codePointCount: " + codePointCount);
}

[실행 결과]

codePointCount: 10
반응형

댓글