導航:首頁 > 編程語言 > java獲取漢字拼音

java獲取漢字拼音

發布時間:2022-05-07 22:46:40

java中怎樣輸入漢字顯示漢字的拼音

java輸入漢語拼音,輸出匹配的漢字,不藉助客戶的選擇,匹配完全正確是幾乎不可能的,尤其是人名,否則拼音輸入法早就演變成無需在輸入漢語拼音後還要選字選詞的操作方式了。

這個屬於自然語言處理的范疇:NLP,暫時還沒有看到過java上的相關應用。
但是,可以參考一下拼音輸入法,這個需求和輸入法很相似啊,不過,現在成熟的輸入法都不能夠完全做到一次性將拼音和漢字匹配成功,這個很有難度,頂多隻是匹配,盡量的匹配。

❷ java怎麼根據漢字獲取字的拼音首字母

獲取首字母需要對漢字表和字母表進行映射,如下示例代碼是以gb2312編碼為入手點,進行匹配的,也可以使用gbk、utf-8等編碼進行匹配,但代碼就完全不同了。

示例代碼如下:

public class FirstLetterUtils {

// 簡體中文的編碼范圍從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 = "";
e.printStackTrace();
}
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 = 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) throws Exception {
System.out.println(cn2py("這是一個獲取首字母的class"));
}
}

❸ java里怎麼輸入漢字,把它轉化為拼音求高手指點

這個是我所做項目中所涉及到的,希望能幫到你。

public class CharacterParser {
private static int[] pyvalue = new int[] { -20319, -20317, -20304, -20295,
-20292, -20283, -20265, -20257, -20242, -20230, -20051, -20036,
-20032, -20026, -20002, -19990, -19986, -19982, -19976, -19805,
-19784, -19775, -19774, -19763, -19756, -19751, -19746, -19741,
-19739, -19728, -19725, -19715, -19540, -19531, -19525, -19515,
-19500, -19484, -19479, -19467, -19289, -19288, -19281, -19275,
-19270, -19263, -19261, -19249, -19243, -19242, -19238, -19235,
-19227, -19224, -19218, -19212, -19038, -19023, -19018, -19006,
-19003, -18996, -18977, -18961, -18952, -18783, -18774, -18773,
-18763, -18756, -18741, -18735, -18731, -18722, -18710, -18697,
-18696, -18526, -18518, -18501, -18490, -18478, -18463, -18448,
-18447, -18446, -18239, -18237, -18231, -18220, -18211, -18201,
-18184, -18183, -18181, -18012, -17997, -17988, -17970, -17964,
-17961, -17950, -17947, -17931, -17928, -17922, -17759, -17752,
-17733, -17730, -17721, -17703, -17701, -17697, -17692, -17683,
-17676, -17496, -17487, -17482, -17468, -17454, -17433, -17427,
-17417, -17202, -17185, -16983, -16970, -16942, -16915, -16733,
-16708, -16706, -16689, -16664, -16657, -16647, -16474, -16470,
-16465, -16459, -16452, -16448, -16433, -16429, -16427, -16423,
-16419, -16412, -16407, -16403, -16401, -16393, -16220, -16216,
-16212, -16205, -16202, -16187, -16180, -16171, -16169, -16158,
-16155, -15959, -15958, -15944, -15933, -15920, -15915, -15903,
-15889, -15878, -15707, -15701, -15681, -15667, -15661, -15659,
-15652, -15640, -15631, -15625, -15454, -15448, -15436, -15435,
-15419, -15416, -15408, -15394, -15385, -15377, -15375, -15369,
-15363, -15362, -15183, -15180, -15165, -15158, -15153, -15150,
-15149, -15144, -15143, -15141, -15140, -15139, -15128, -15121,
-15119, -15117, -15110, -15109, -14941, -14937, -14933, -14930,
-14929, -14928, -14926, -14922, -14921, -14914, -14908, -14902,
-14894, -14889, -14882, -14873, -14871, -14857, -14678, -14674,
-14670, -14668, -14663, -14654, -14645, -14630, -14594, -14429,
-14407, -14399, -14384, -14379, -14368, -14355, -14353, -14345,
-14170, -14159, -14151, -14149, -14145, -14140, -14137, -14135,
-14125, -14123, -14122, -14112, -14109, -14099, -14097, -14094,
-14092, -14090, -14087, -14083, -13917, -13914, -13910, -13907,
-13906, -13905, -13896, -13894, -13878, -13870, -13859, -13847,
-13831, -13658, -13611, -13601, -13406, -13404, -13400, -13398,
-13395, -13391, -13387, -13383, -13367, -13359, -13356, -13343,
-13340, -13329, -13326, -13318, -13147, -13138, -13120, -13107,
-13096, -13095, -13091, -13076, -13068, -13063, -13060, -12888,
-12875, -12871, -12860, -12858, -12852, -12849, -12838, -12831,
-12829, -12812, -12802, -12607, -12597, -12594, -12585, -12556,
-12359, -12346, -12320, -12300, -12120, -12099, -12089, -12074,
-12067, -12058, -12039, -11867, -11861, -11847, -11831, -11798,
-11781, -11604, -11589, -11536, -11358, -11340, -11339, -11324,
-11303, -11097, -11077, -11067, -11055, -11052, -11045, -11041,
-11038, -11024, -11020, -11019, -11018, -11014, -10838, -10832,
-10815, -10800, -10790, -10780, -10764, -10587, -10544, -10533,
-10519, -10331, -10329, -10328, -10322, -10315, -10309, -10307,
-10296, -10281, -10274, -10270, -10262, -10260, -10256, -10254 };
public static String[] pystr = new String[] { "a", "ai", "an", "ang",
"ao", "ba", "", "ban", "bang", "bao", "bei", "ben", "beng",
"bi", "bian", "biao", "bie", "bin", "bing", "bo", "bu", "ca",
"cai", "can", "cang", "cao", "ce", "ceng", "cha", "chai", "chan",
"chang", "chao", "che", "chen", "cheng", "chi", "chong", "chou",
"chu", "chuai", "chuan", "chuang", "chui", "chun", "chuo", "ci",
"cong", "cou", "cu", "cuan", "cui", "cun", "cuo", "da", "dai",
"dan", "dang", "", "de", "deng", "di", "dian", "diao", "die",
"ding", "diu", "dong", "dou", "", "an", "i", "n", "o",
"e", "en", "er", "fa", "fan", "fang", "fei", "fen", "feng", "fo",
"fou", "fu", "ga", "gai", "gan", "gang", "gao", "ge", "gei", "gen",
"geng", "gong", "gou", "gu", "gua", "guai", "guan", "guang", "gui",
"gun", "guo", "ha", "hai", "han", "hang", "hao", "he", "hei",
"hen", "heng", "hong", "hou", "hu", "hua", "huai", "huan", "huang",
"hui", "hun", "huo", "ji", "jia", "jian", "jiang", "jiao", "jie",
"jin", "jing", "jiong", "jiu", "ju", "juan", "jue", "jun", "ka",
"kai", "kan", "kang", "kao", "ke", "ken", "keng", "kong", "kou",
"ku", "kua", "kuai", "kuan", "kuang", "kui", "kun", "kuo", "la",
"lai", "lan", "lang", "lao", "le", "lei", "leng", "li", "lia",
"lian", "liang", "liao", "lie", "lin", "ling", "liu", "long",
"lou", "lu", "lv", "luan", "lue", "lun", "luo", "ma", "mai", "man",
"mang", "mao", "me", "mei", "men", "meng", "mi", "mian", "miao",
"mie", "min", "ming", "miu", "mo", "mou", "mu", "na", "nai", "nan",
"nang", "nao", "ne", "nei", "nen", "neng", "ni", "nian", "niang",
"niao", "nie", "nin", "ning", "niu", "nong", "nu", "nv", "nuan",
"nue", "nuo", "o", "ou", "pa", "pai", "pan", "pang", "pao", "pei",
"pen", "peng", "pi", "pian", "piao", "pie", "pin", "ping", "po",
"pu", "qi", "qia", "qian", "qiang", "qiao", "qie", "qin", "qing",
"qiong", "qiu", "qu", "quan", "que", "qun", "ran", "rang", "rao",
"re", "ren", "reng", "ri", "rong", "rou", "ru", "ruan", "rui",
"run", "ruo", "sa", "sai", "san", "sang", "sao", "se", "sen",
"seng", "sha", "shai", "shan", "shang", "shao", "she", "shen",
"sheng", "shi", "shou", "shu", "shua", "shuai", "shuan", "shuang",
"shui", "shun", "shuo", "si", "song", "sou", "su", "suan", "sui",
"sun", "suo", "ta", "tai", "tan", "tang", "tao", "te", "teng",
"ti", "tian", "tiao", "tie", "ting", "tong", "tou", "tu", "tuan",
"tui", "tun", "tuo", "wa", "wai", "wan", "wang", "wei", "wen",
"weng", "wo", "wu", "xi", "xia", "xian", "xiang", "xiao", "xie",
"xin", "xing", "xiong", "xiu", "xu", "xuan", "xue", "xun", "ya",
"yan", "yang", "yao", "ye", "yi", "yin", "ying", "yo", "yong",
"you", "yu", "yuan", "yue", "yun", "za", "zai", "zan", "zang",
"zao", "ze", "zei", "zen", "zeng", "zha", "zhai", "zhan", "zhang",
"zhao", "zhe", "zhen", "zheng", "", "zhong", "zhou", "zhu",
"zhua", "zhuai", "zhuan", "zhuang", "zhui", "zhun", "zhuo", "zi",
"zong", "zou", "zu", "zuan", "zui", "zun", "zuo" };
private StringBuilder buffer;
private String resource;

private static CharacterParser characterParser = new CharacterParser();

public static CharacterParser getInstance() {
return characterParser;
}

public String getResource() {
return resource;
}

public void setResource(String resource) {
this.resource = resource;
}

/**
* 漢字轉成ASCII碼
*
* @param chs
* @return
*/
private int getChsAscii(String chs) {
int asc = 0;
try {
byte[] bytes = chs.getBytes("gb2312");
if (bytes == null || bytes.length > 2 || bytes.length <= 0) { // 錯誤
throw new RuntimeException("illegal resource string");
}
if (bytes.length == 1) { // 英文字元
asc = bytes[0];
}
if (bytes.length == 2) { // 中文字元
int hightByte = 256 + bytes[0];
int lowByte = 256 + bytes[1];
asc = (256 * hightByte + lowByte) - 256 * 256;
}
} catch (Exception e) {
System.out
.println("ERROR:ChineseSpelling.class-getChsAscii(String chs)"
+ e);
}
return asc;
}

/**
* 單字解析
*
* @param str
* @return
*/
public String convert(String str) {
String result = null;
int ascii = getChsAscii(str);
if (ascii > 0 && ascii < 160) {
result = String.valueOf((char) ascii);
} else {
for (int i = (pyvalue.length - 1); i >= 0; i--) {
if (pyvalue[i] <= ascii) {
result = pystr[i];
break;
}
}
}
return result;
}

/**
* 片語解析
*
* @param chs
* @return
*/
public String getSelling(String chs) {
String key, value;
buffer = new StringBuilder();
for (int i = 0; i < chs.length(); i++) {
key = chs.substring(i, i + 1);
if (key.getBytes().length >= 2) {
value = (String) convert(key);
if (value == null) {
value = "unknown";
}
} else {
value = key;
}
buffer.append(value);
}
return buffer.toString();
}

public String getSpelling() {
return this.getSelling(this.getResource());
}

public static void main(String[] args) {
CharacterParser parser = CharacterParser.getInstance();
System.out.println(parser.getSelling("中華人民共和國"));
}
}

❹ 如何用java獲取中文拼音的首字母

import java.io.UnsupportedEncodingException;
/**
* 取得給定漢字串的首字母串,即聲母串
* Title: ChineseCharToEn
* @date 2004-02-19 註:只支持GB2312字元集中的漢字
*/
public final class ChineseCharToEn {
private final static int[] li_SecPosValue = { 1601, 1637, 1833, 2078, 2274,
2302, 2433, 2594, 2787, 3106, 3212, 3472, 3635, 3722, 3730, 3858,
4027, 4086, 4390, 4558, 4684, 4925, 5249, 5590 };
private final static String[] lc_FirstLetter = { "a", "b", "c", "d", "e",
"f", "g", "h", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
"t", "w", "x", "y", "z" };
/**
* 取得給定漢字串的首字母串,即聲母串
* @param str 給定漢字串
* @return 聲母串
*/
public String getAllFirstLetter(String str) {
if (str == null || str.trim().length() == 0) {
return "";
}
String _str = "";
for (int i = 0; i < str.length(); i++) {
_str = _str + this.getFirstLetter(str.substring(i, i + 1));
}
return _str;
}
/**
* 取得給定漢字的首字母,即聲母
* @param chinese 給定的漢字
* @return 給定漢字的聲母
*/
public String getFirstLetter(String chinese) {
if (chinese == null || chinese.trim().length() == 0) {
return "";
}
chinese = this.conversionStr(chinese, "GB2312", "ISO8859-1");
if (chinese.length() > 1) // 判斷是不是漢字
{
int li_SectorCode = (int) chinese.charAt(0); // 漢字區碼
int li_PositionCode = (int) chinese.charAt(1); // 漢字位碼
li_SectorCode = li_SectorCode - 160;
li_PositionCode = li_PositionCode - 160;
int li_SecPosCode = li_SectorCode * 100 + li_PositionCode; // 漢字區位碼
if (li_SecPosCode > 1600 && li_SecPosCode < 5590) {
for (int i = 0; i < 23; i++) {
if (li_SecPosCode >= li_SecPosValue[i]
&& li_SecPosCode < li_SecPosValue[i + 1]) {
chinese = lc_FirstLetter[i];
break;
}
}
} else // 非漢字字元,如圖形符號或ASCII碼
{
chinese = this.conversionStr(chinese, "ISO8859-1", "GB2312");
chinese = chinese.substring(0, 1);
}
}
return chinese;
}
/**
* 字元串編碼轉換
* @param str 要轉換編碼的字元串
* @param charsetName 原來的編碼
* @param toCharsetName 轉換後的編碼
* @return 經過編碼轉換後的字元串
*/
private String conversionStr(String str, String charsetName,String toCharsetName) {
try {
str = new String(str.getBytes(charsetName), toCharsetName);
} catch (UnsupportedEncodingException ex) {
System.out.println("字元串編碼轉換異常:" + ex.getMessage());
}
return str;
}
public static void main(String[] args) {
ChineseCharToEn cte = new ChineseCharToEn();
System.out.println("獲取拼音首字母:"+ cte.getAllFirstLetter("北京聯席辦"));
}
}

❺ java可以通過漢字得到帶聲調的拼音嗎

packagetest;
importnet.sourceforge.pinyin4j.PinyinHelper;
importnet.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
importnet.sourceforge.pinyin4j.format.HanyuPinyinToneType;
importnet.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
importnet.sourceforge.pinyin4j.format.exception.;
publicclassTest{
publicstaticvoidmain(String[]args)throws{
HanyuPinyinOutputFormatformat=newHanyuPinyinOutputFormat();
format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
String[]pinyinHead=PinyinHelper.toHanyuPinyinStringArray('度',format);
for(Stringstring:pinyinHead){
System.out.print(string+"、");
}
}
}

輸出:dù、ó、ò

❻ java如何獲取漢字的拼音字母

獲取首字母需要對漢字表和字母表進行映射,如下示例代碼是以gb2312編碼為入手點,進行匹配的,也可以使用gbk、utf-8等編碼進行匹配,但代碼就完全不同了。

示例代碼如下:

public class FirstLetterUtils {

// 簡體中文的編碼范圍從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 = "";
e.printStackTrace();
}
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 = 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) throws Exception {
System.out.println(cn2py("這是一個獲取首字母的class"));
}
}

❼ (JAVA)如何獲取一個漢字的聲母和韻母

通用的辦法,就是將聲母、韻母與漢字相對應存儲於文件(通常是資料庫)中,在程序里通過查找漢字取出與漢字對應的聲母韻母。演算法的考究最多也就在數據的存儲方式和查找方式上

❽ java 中如何獲得String name="李鵬" 首漢字的首拼音字母

我以前做過了.

實現方法一:具體的原理是有一個輸入法字型檔,文本文件.在裡面檢索.
實現方法二,用一個函數取首字母的拼音,函數如下:(下面的函數是用VB的,因為JAVA的語法我不知道.)

Public Function py(mystr As String) As String
i = Asc(mystr)
Select Case i
Case -20319 To -20284: py = "A"
Case -20283 To -19776: py = "B"
Case -19775 To -19219: py = "C"
Case -19218 To -18711: py = "D"
Case -18710 To -18527: py = "E"
Case -18526 To -18240: py = "F"
Case -18239 To -17923: py = "G"
Case -17922 To -17418: py = "H"
Case -17417 To -16475: py = "J"
Case -16474 To -16213: py = "K"
Case -16212 To -15641: py = "L"
Case -15640 To -15166: py = "M"
Case -15165 To -14923: py = "N"
Case -14922 To -14915: py = "O"
Case -14914 To -14631: py = "P"
Case -14630 To -14150: py = "Q"
Case -14149 To -14091: py = "R"
Case -14090 To -13319: py = "S"
Case -13318 To -12839: py = "T"
Case -12838 To -12557: py = "W"
Case -12556 To -11848: py = "X"
Case -11847 To -11056: py = "Y"
Case -11055 To -10247: py = "Z"
Case Else: py = mystr
End Select
End Function

❾ 如何用JAVA獲取中文拼音的首字母

packagetest;

publicclassFirstLetterUtil{
privatestaticintBEGIN=45217;
privatestaticintEND=63486;//按照聲母表示,這個表是在GB2312中的出現的第一個漢字,也就是說「啊」是代表首字母a的第一個漢字。//i,u,v都不做聲母,自定規則跟隨前面的字母
privatestaticchar[]chartable={'啊','芭','擦','搭','蛾','發','噶','哈','哈','擊','喀','垃','媽','拿','哦','啪','期','然','撒','塌','塌','塌','挖','昔','壓','匝',};
//二十六個字母區間對應二十七個端點
//GB2312碼漢字區間十進製表示
privatestaticint[]table=newint[27];
//對應首字母區間表
privatestaticchar[]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(inti=0;i<26;i++){
table[i]=gbValue(chartable[i]);//得到GB2312碼的首字母區間端點表,十進制。

}
table[26]=END;//區間表結尾
}

publicstaticvoidmain(String[]args){
System.out.println(FirstLetterUtil.getFirstLetter("你好"));;
}
/**
*根據一個包含漢字的字元串返回一個漢字拼音首字母的字元串最重要的一個方法,思路如下:一個個字元讀入、判斷、輸出
*/
(StringsourceStr){
Stringresult="";
Stringstr=sourceStr.toLowerCase();
intStrLength=str.length();
inti;
try{
for(i=0;i<StrLength;i++){
result+=Char2Initial(str.charAt(i));
}
}catch(Exceptione){
result="";
}
returnresult;
}

/**
*輸入字元,得到他的聲母,英文字母返回對應的大寫字母,其他非簡體漢字返回'0'
*/
privatestaticcharChar2Initial(charch){
//對英文字母的處理:小寫字母轉換為大寫,大寫的直接返回
if(ch>='a'&&ch<='z'){
returnch;
}
if(ch>='A'&&ch<='Z'){
returnch;
}//對非英文字母的處理:轉化為首字母,然後判斷是否在碼表范圍內,
//若不是,則直接返回。
//若是,則在碼表內的進行判斷。
intgb=gbValue(ch);//漢字轉換首字母

if((gb<BEGIN)||(gb>END))//在碼表區間之前,直接返回
{
returnch;
}
inti;
for(i=0;i<26;i++){//判斷匹配碼表區間,匹配到就break,判斷區間形如「[,)」
if((gb>=table[i])&&(gb<table[i+1])){
break;
}
}
if(gb==END){//補上GB2312區間最右端
i=25;
}
returninitialtable[i];//在碼表區間中,返回首字母
}

/**
*取出漢字的編碼cn漢字
*/
privatestaticintgbValue(charch){//將一個漢字(GB2312)轉換為十進製表示。
Stringstr=newString();
str+=ch;
try{
byte[]bytes=str.getBytes("GB2312");
if(bytes.length<2){
return0;
}
return(bytes[0]<<8&0xff00)+(bytes[1]&0xff);
}catch(Exceptione){
return0;
}
}
}

閱讀全文

與java獲取漢字拼音相關的資料

熱點內容
安卓機內存刪除怎麼恢復 瀏覽:329
Qt環境的編譯軟體放到linux 瀏覽:212
聯創列印系統怎麼連接伺服器 瀏覽:935
杭州行政命令 瀏覽:160
如何查找伺服器日誌 瀏覽:801
加密的鑰匙扣怎麼寫 瀏覽:579
文件夾更新不了怎麼辦 瀏覽:475
壓縮機指示燈亮是什麼原因 瀏覽:956
什麼app訂酒店半價 瀏覽:765
中老年解壓神器 瀏覽:243
訊飛語音ttsandroid 瀏覽:468
腰椎壓縮性骨折術後能坐車嗎 瀏覽:507
python類裝飾器參數 瀏覽:347
均線pdf微盤 瀏覽:791
女生喜歡玩的解壓游戲 瀏覽:442
支付寶暗號加密操作 瀏覽:134
柯潔在哪個app下圍棋 瀏覽:751
平板用什麼app看內在美 瀏覽:609
cad計算機命令 瀏覽:173
郵箱設置域名伺服器錯誤什麼意思 瀏覽:671