How To Convert To Korean Initials
Solution 1:
I don't think it is normal behaviour in a Korean app to have intellisense from just an intial character - it seems to usually be done with a full Jamo. However, I see no reason why you shouldn't do it - so lets do it.
Firstly, you missed the double initials. These are different from the singles, as they do require a different key press (usually shift+character). Anyhow, your list of initials should be:
ㄱㄲㄴㄷㄸㄹㅁㅂㅃㅅㅆㅇㅈㅉㅊㅋㅌㅍㅎ
The first thing you need to do when an initial is typed in is to get the range of characters that start with that particular initial.
By looking at the Windows Character Map, I one can see that the first letter, Ka (가) is at Unicode point 0xAC00
(or decimal, 44032
), so to get any range, this value will have to be added to any calculation we do. So you should have a constant called FirstLetter
or whatever, and its value should be 44032
.
까 is at 0xAE4C
(or decimal 44620
), so we now have a multiplier (i.e. the number of characters beginning with any particular initial - which is the same for all initials, so that is 44620-44032
which is 588
.
We now have enough information to implement your intellisense. If you have the initialisms in an array, we can use the index of the typed character in that array to find the range of characters that start with it.
So if we start with ㄱ, we obviously want to return everything from 가 to 깋. All 588 of them. So ㄱ gives us a '0', so we have
startCodePoint = index * 588 + 44032// = 0 * 588 + 44032 == 44032 == 가
endCodePoint = (index + 1) * 588 + 44032// this will include 까
And then you can just check that a particular character starts with 'ㄱ' by checking
if(charcode >= startCodePoint && charcode < endCodePoint) { ... }
where charcode
is the first character of one of the items in your intellisense list.
Use a similar methodology to find out how to check that a character starts with say '가'. Everything is in order in Unicode, so this is a very simple task.
To get the initial letter of any character, you can use the above formula in reverse.
i.e.
- Get the unicode value of the first character (e.g. 각)
- Subtract 44032 from this value.
- Divide this value by 588.
- Use that value as the index to retrieve the intial character from the list of initials.
e.g.
Stringinitials="ㄱㄲㄴㄷㄸㄹㅁㅂㅃㅅㅆㅇㅈㅉㅊㅋㅌㅍㅎ";
intvalue= character.codePointAt(0);
value = (value - 44032) / 588;
Stringinitial= initials.substring(value, 1);
Solution 2:
The international way is to fetch the Unicode int code. This in general might be more than one 16 bit char.
int codePoint = strng.codePointAt(0);
int indexToRest = Character.charCount(codePoint); // For your information
Post a Comment for "How To Convert To Korean Initials"