어제 오늘 내일

[JavaAPI] String 클래스 메소드와 예제(3) - Unicode 처리(2) 본문

IT/Java

[JavaAPI] String 클래스 메소드와 예제(3) - Unicode 처리(2)

hi.anna 2016. 6. 12. 02:53


이번에는 앞에서 소개한 32bit로 인코딩된 문자를

Java 코드를 통해 확인해 보도록 하겠다.

([JavaAPI] String 클래스 메소드와 예제(2) - Unicode 처리(1) 참조)




public boolean equals(Object anObject)


Returns the length of this string. The length is equal to the number of Unicode code units in the string.

String 클래스의 length() 메소드는 string의 길이를 리턴한다. 정확히는 unicode code unit의 숫자를 리턴한다.


결과 : 

String 🂡 length : 2

String A length : 1


위 예제에서 1F0A1은 32비트 '🂡' 문자를 표현하는 코드포인트이고, 0041은 16비트 'A' 문자를 표현하는 코드포인트이다.

32비트 문자의 경우 UTF-16 인코딩에 따라 2개의 16진수로 표현되기 때문에 length가 2가 되었다. 16비트 문자인 'A'의 경우 일반적으로 알고 있는 바와 같이 length가 1이 산출되었다.



public char charAt(int index)

Returns the char value at the specified index. An index ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing.

If the char value specified by the index is a surrogate, the surrogate value is returned.

charAt() 메소드는 argument로 지정한 index의 char를 리턴한다. 만약, 해당 인덱스가 surrogate값을 가지면, surrogate값이 리턴된다.


결과 : 
A
55356
56481
65

위 예제에서 32비트 문자의 0번째 인덱스와 1번째 인덱스의 char는 깨진 문자를 리턴하였다. 그래서, 그 코드값을 확인하기 위해 해당 char의 int값을 출력해보니, 0번째 인덱스는 55356, 1번째 인덱스는 56481이 출력되었다.
지난 포스팅에서 '1F0A1'의 high-surrogate값이 55356, low-surrogate값이 56481이었음을 기억하라.





public int codePointAt(int index)
Returns the character (Unicode code point) at the specified index. The index refers to char values (Unicode code units) and ranges from 0 to length()- 1.
If the char value specified at the given index is in the high-surrogate range, the following index is less than the length of this String, and the char value at the following index is in the low-surrogate range, then the supplementary code point corresponding to this surrogate pair is returned. Otherwise, the char value at the given index is returned.
codePointAt 메소드는 argument로 지정한 index의 코드포인트 값을 리턴한다.
32비트 문자의 경우 high-surrogate값과 low-surrogate 두개의 값이 존재하며, 해당 문자의 0번째 인덱스 값에 high-surrogate값이, 1번째 인덱스 값이 low-surrogate값이 존재하게 된다.
그래서, codePointAt 메소드의 인덱스가 high-surrogate값을 가리킬 경우, 해당 문자의 code point를 리턴하고, low-surrogate값을 가리킬 경우 해당 인덱스의 code point를 리턴한다.
아래의 예제를 보자.

결과 : 

127137

56481

65


32비트 문자의 high-surrogate값을 가리키는 codePointAt(0)의 결과값은 127137. 원래 입력된 1F0A1의 10진수 값이다. 하지만, 32비트 문자의 low-surroagate값을 가리키는 codePointAt(1)의 결과값은 56481로 low-surrogate 값을 리턴하였다.





public int codePointBefore(int index)

Returns the character (Unicode code point) before the specified index. The index refers to char values (Unicode code units) and ranges from 1 to length.

If the char value at (index - 1) is in the low-surrogate range, (index - 2) is not negative, and the char value at (index - 2) is in the high-surrogate range, then the supplementary code point value of the surrogate pair is returned. If the char value at index - 1 is an unpaired low-surrogate or a high-surrogate, the surrogate value is returned.

argument로 지정된 인덱스 이전의 코드 포인트를 리턴한다. index-1이 low-surrogate인 경우 해당 문자(32bit)의 코드포인트를 리턴한다.


결과 : 
55356
127137
65


public int codePointCount(int beginIndex, int endIndex)
Returns the number of Unicode code points in the specified text range of this String. The text range begins at the specified beginIndex and extends to the char at index endIndex - 1. Thus the length (in chars) of the text range is endIndex-beginIndex. Unpaired surrogates within the text range count as one code point each.

beginIndex부터 endIndex까지의 코드포인트 갯수를 리턴한다. 32bit문자는 2개의 인덱스를 차지하는데 그 중 하나의 인덱스만 범위로 지정한 경우 1개의 코드 포인트로 계산한다.


결과 : 

1

1

1



public int offsetByCodePoints(int index, int codePointOffset)

Returns the index within this String that is offset from the given index by codePointOffset code points. Unpaired surrogates within the text range given by index and codePointOffset count as one code point each.

argument로 지정한 index위치부터 codePointOffset만큼 code point단위로 이동한 위치의 index를 리턴한다. 즉, 32bit 문자의 경우 0번째 인덱스부터 codePointOffset 1만큼 이동한 index의 위치는 2이다. 하나의 문자, 즉 하나의 code point가 2개의 인덱스를 차지하고 있기때문에.


결과 : 

2

2

1


예제에서 두번째 케이스의 경우 argument로 입력받은 index의 위치가 low-surrogate인 경우 1개의 code point로 처리한다.




String 클래스에서 32비트 문자 처리관련 메소드들을 살펴보았다.

일반적으로 한국어나 영어를 처리할 때는 이러한 내용을 몰라도 별 차이가 없다. 하지만, 요즘처럼 다국어를 지원해야 하는 경우에는 반드시 신경써서 알아야 할 부분인 것 같다.

반응형
Comments