javaは漢字文字列のピンインの頭文字を取り出します。

3840 ワード

/**
* Created by IntelliJ IDEA.
* Date: 2004-5-17
* Time: 10:59:59
* ClassDescription:
*/



import java.lang.*;

public class GB2Alpha {

// Z , 27
//i, u, v ,
private char[] chartable =
{
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '
};

private char[] alphatable =
{
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',

'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
};


private int[] table = new int[27];

//
{
for (int i = 0; i < 27; ++i) {
table[i] = gbValue(chartable[i]);
}
}

public GB2Alpha() {

}

// , , ,
//
// '0'

public char Char2Alpha(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 < table[0])
return '0';


int i;
for (i = 0; i < 26; ++i) {
if (match(i, gb))
break;
}

if (i >= 26)
return '0';
else
return alphatable[i];
}

//
public String String2Alpha(String SourceStr) {
String Result = "";
int StrLength = SourceStr.length();
int i;
try {
for (i = 0; i < StrLength; i++) {
Result += Char2Alpha(SourceStr.charAt(i));
}
} catch (Exception e) {
Result = "";
}
return Result;
}

private boolean match(int i, int gb) {
if (gb < table[i])
return false;

int j = i + 1;

// Z
while (j < 26 && (table[j] == table[i]))
++j;

if (j == 26)
return gb <= table[j];
else
return gb < table[j];

}

//
private int gbValue(char ch) {
String str = new String();
str += 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) {
com.mkrx.GB2Alpha obj1 = new com.mkrx.GB2Alpha();
System.out.println(obj1.String2Alpha(" : !"));
return;
}
}