문자 병음 이니셜 가져오기
4181 단어 얻다
/**
* @date 2010-1-22
* @bugs
*/
public class PinyinConv {
// B0A1(45217) F7FE(63486)
private static int BEGIN = 45217;
private static int END = 63486;
// , GB2312 , “ ” a 。
// i, u, v ,
private static char[] chartable = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', };
//
// GB2312
private static int[] table = new int[27];
//
private static char[] initialtable = { 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'h', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
't', 't', 'w', 'x', 'y', 'z'};
//
static {
for (int i = 0; i < 26; i++) {
table[i] = gbValue(chartable[i]);// GB2312 , 。
}
table[26] = END;//
}
// ------------------------public ------------------------
/**
* , : 、 、
*/
public static String cn2py(String SourceStr) {
String Result = "";
int StrLength = SourceStr.length();
int i;
try {
for (i = 0; i < StrLength; i++) {
Result += Char2Initial(SourceStr.charAt(i));
}
} catch (Exception e) {
Result = "";
}
return Result;
}
// ------------------------private ------------------------
/**
* , , , '0'
*
*/
private static char Char2Initial(char ch) {
// : ,
if (ch >= 'a' && ch <= 'z')
return (char) (ch - 'a' + 'A');
if (ch >= 'A' && ch <= 'Z')
return ch;
// : , ,
// , 。
// , 。
int gb = gbValue(ch);//
if ((gb < BEGIN) || (gb > END))// ,
return ch;
int i;
for (i = 0; i < 26; i++) {// , break, “[,)”
if ((gb >= table[i]) && (gb < table[i+1]))
break;
}
if (gb==END) {// GB2312
i=25;
}
return initialtable[i]; // ,
}
/**
* cn
*/
private static int gbValue(char ch) {// (GB2312) 。
String str = String.valueOf(ch);
try {
byte[] bytes = str.getBytes("GB2312");
if (bytes.length < 2)
return 0;
return (bytes[0] << 8 & 0xff00) + (bytes[1] & 0xff);
} catch (Exception e) {
return 0;
}
}
public static void main(String[] args) throws Exception {
System.out.println(cn2py(" IT , , ,IBM "));
}
}
이 단락의 코드가 인터넷에 올라오는 원리는 한자를 통한 GB2312의 인코딩은 병음 이니셜의 순서대로 배열되어 구간 비교를 통해 이니셜을 얻는 것이지만, 여기서도 다음자는 구분할 수 없고 문자의 기본 음절의 이니셜만 반영된다는 것을 알 수 있다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
vue 단일 파일 구성 요소에서 $refs를 가져올 수 없는 문제웹pack+vue를 배우는 데 부딪힌 큰 구덩이를 기록해 보세요. 이 구덩이를 밟는 것은 저의 재능과 학문이 얕은 표현입니다. 이를 경계로 삼겠습니다.왜냐하면 이 구덩이는 정말 너무 구덩이야! 코드 header.ht...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.