2010년 7월 28일 수요일

장보기 어플 초성검색 기능

장보기 어플의 항목등록을 위해 자바로 구현된 초성검색 로직이다.

public static String getInitialConsonant(String str) {
int value = 0;
int initial, consonant = 0;
String[] arr_consonant =
new String[] {"ㄱ","ㄲ","ㄴ","ㄷ","ㄸ","ㄹ"
,"ㅁ","ㅂ","ㅃ","ㅅ","ㅆ","ㅇ","ㅈ"
,"ㅉ","ㅊ","ㅋ","ㅌ","ㅍ","ㅎ"};
String result = "";

for(int i = 0 ; i < str.length(); i++) { value = str.subSequence(i, i+1).hashCode() - 0xAC00; consonant = value % 28; initial = (int)(((value - consonant) / 28) / 21); if(initial > 0) result += arr_consonant[initial];
else result += str.substring(i, i+1);
}

return result;
}

댓글 없음:

댓글 쓰기