java汉字转拼音代码,java汉字编码转换

求JAVA 汉字转拼音代码,谢谢!

import java.util.Iterator;

创新互联建站是一家专注于成都网站设计、成都做网站、外贸网站建设与策划设计,晋源网站建设哪家好?创新互联建站做网站,专注于网站建设十多年,网设计领域的专业建站公司;建站业务涵盖:晋源等地区。晋源做网站价格咨询:13518219792

import java.util.LinkedHashMap;

import java.util.Set;

public class ConverPinYin

{

private static LinkedHashMap spellMap = null;

static

{

if (spellMap == null)

{

spellMap = new LinkedHashMap(400);

}

initialize();

}

private static void spellPut(String spell, int ascii)

{

spellMap.put(spell, new Integer(ascii));

}

private static void initialize()

{

spellPut("a", -20319);

spellPut("ai", -20317);

spellPut("an", -20304);

spellPut("ang", -20295);

spellPut("ao", -20292);

spellPut("ba", -20283);

spellPut("bai", -20265);

spellPut("ban", -20257);

spellPut("bang", -20242);

spellPut("bao", -20230);

spellPut("bei", -20051);

spellPut("ben", -20036);

spellPut("beng", -20032);

spellPut("bi", -20026);

spellPut("bian", -20002);

spellPut("biao", -19990);

spellPut("bie", -19986);

spellPut("bin", -19982);

spellPut("bing", -19976);

spellPut("bo", -19805);

spellPut("bu", -19784);

spellPut("ca", -19775);

spellPut("cai", -19774);

spellPut("can", -19763);

spellPut("cang", -19756);

spellPut("cao", -19751);

spellPut("ce", -19746);

spellPut("ceng", -19741);

spellPut("cha", -19739);

spellPut("chai", -19728);

spellPut("chan", -19725);

spellPut("chang", -19715);

spellPut("chao", -19540);

spellPut("che", -19531);

spellPut("chen", -19525);

spellPut("cheng", -19515);

spellPut("chi", -19500);

spellPut("chong", -19484);

spellPut("chou", -19479);

spellPut("chu", -19467);

spellPut("chuai", -19289);

spellPut("chuan", -19288);

spellPut("chuang", -19281);

spellPut("chui", -19275);

spellPut("chun", -19270);

spellPut("chuo", -19263);

spellPut("ci", -19261);

spellPut("cong", -19249);

spellPut("cou", -19243);

spellPut("cu", -19242);

spellPut("cuan", -19238);

spellPut("cui", -19235);

spellPut("cun", -19227);

spellPut("cuo", -19224);

spellPut("da", -19218);

spellPut("dai", -19212);

spellPut("dan", -19038);

spellPut("dang", -19023);

spellPut("dao", -19018);

spellPut("de", -19006);

spellPut("deng", -19003);

spellPut("di", -18996);

spellPut("dian", -18977);

spellPut("diao", -18961);

spellPut("die", -18952);

spellPut("ding", -18783);

spellPut("diu", -18774);

spellPut("dong", -18773);

spellPut("dou", -18763);

spellPut("du", -18756);

spellPut("duan", -18741);

spellPut("dui", -18735);

spellPut("dun", -18731);

spellPut("duo", -18722);

spellPut("e", -18710);

spellPut("en", -18697);

spellPut("er", -18696);

spellPut("fa", -18526);

spellPut("fan", -18518);

spellPut("fang", -18501);

spellPut("fei", -18490);

spellPut("fen", -18478);

spellPut("feng", -18463);

spellPut("fo", -18448);

spellPut("fou", -18447);

spellPut("fu", -18446);

spellPut("ga", -18239);

spellPut("gai", -18237);

spellPut("gan", -18231);

spellPut("gang", -18220);

spellPut("gao", -18211);

spellPut("ge", -18201);

spellPut("gei", -18184);

spellPut("gen", -18183);

spellPut("geng", -18181);

spellPut("gong", -18012);

spellPut("gou", -17997);

spellPut("gu", -17988);

spellPut("gua", -17970);

spellPut("guai", -17964);

spellPut("guan", -17961);

spellPut("guang", -17950);

spellPut("gui", -17947);

spellPut("gun", -17931);

spellPut("guo", -17928);

spellPut("ha", -17922);

spellPut("hai", -17759);

spellPut("han", -17752);

spellPut("hang", -17733);

spellPut("hao", -17730);

spellPut("he", -17721);

spellPut("hei", -17703);

spellPut("hen", -17701);

spellPut("heng", -17697);

spellPut("hong", -17692);

spellPut("hou", -17683);

spellPut("hu", -17676);

spellPut("hua", -17496);

spellPut("huai", -17487);

spellPut("huan", -17482);

spellPut("huang", -17468);

spellPut("hui", -17454);

spellPut("hun", -17433);

spellPut("huo", -17427);

spellPut("ji", -17417);

spellPut("jia", -17202);

spellPut("jian", -17185);

spellPut("jiang", -16983);

spellPut("jiao", -16970);

spellPut("jie", -16942);

spellPut("jin", -16915);

spellPut("jing", -16733);

spellPut("jiong", -16708);

spellPut("jiu", -16706);

spellPut("ju", -16689);

spellPut("juan", -16664);

spellPut("jue", -16657);

spellPut("jun", -16647);

spellPut("ka", -16474);

spellPut("kai", -16470);

spellPut("kan", -16465);

spellPut("kang", -16459);

spellPut("kao", -16452);

spellPut("ke", -16448);

spellPut("ken", -16433);

spellPut("keng", -16429);

spellPut("kong", -16427);

spellPut("kou", -16423);

spellPut("ku", -16419);

spellPut("kua", -16412);

spellPut("kuai", -16407);

spellPut("kuan", -16403);

spellPut("kuang", -16401);

spellPut("kui", -16393);

spellPut("kun", -16220);

spellPut("kuo", -16216);

spellPut("la", -16212);

spellPut("lai", -16205);

spellPut("lan", -16202);

spellPut("lang", -16187);

spellPut("lao", -16180);

spellPut("le", -16171);

spellPut("lei", -16169);

spellPut("leng", -16158);

spellPut("li", -16155);

spellPut("lia", -15959);

spellPut("lian", -15958);

spellPut("liang", -15944);

spellPut("liao", -15933);

spellPut("lie", -15920);

spellPut("lin", -15915);

spellPut("ling", -15903);

spellPut("liu", -15889);

spellPut("long", -15878);

spellPut("lou", -15707);

spellPut("lu", -15701);

spellPut("lv", -15681);

spellPut("luan", -15667);

spellPut("lue", -15661);

spellPut("lun", -15659);

spellPut("luo", -15652);

spellPut("ma", -15640);

spellPut("mai", -15631);

spellPut("man", -15625);

spellPut("mang", -15454);

spellPut("mao", -15448);

spellPut("me", -15436);

spellPut("mei", -15435);

spellPut("men", -15419);

spellPut("meng", -15416);

spellPut("mi", -15408);

spellPut("mian", -15394);

spellPut("miao", -15385);

spellPut("mie", -15377);

spellPut("min", -15375);

spellPut("ming", -15369);

spellPut("miu", -15363);

spellPut("mo", -15362);

spellPut("mou", -15183);

spellPut("mu", -15180);

spellPut("na", -15165);

spellPut("nai", -15158);

spellPut("nan", -15153);

spellPut("nang", -15150);

spellPut("nao", -15149);

spellPut("ne", -15144);

spellPut("nei", -15143);

spellPut("nen", -15141);

spellPut("neng", -15140);

spellPut("ni", -15139);

spellPut("nian", -15128);

spellPut("niang", -15121);

spellPut("niao", -15119);

spellPut("nie", -15117);

spellPut("nin", -15110);

spellPut("ning", -15109);

spellPut("niu", -14941);

spellPut("nong", -14937);

spellPut("nu", -14933);

spellPut("nv", -14930);

spellPut("nuan", -14929);

spellPut("nue", -14928);

spellPut("nuo", -14926);

spellPut("o", -14922);

spellPut("ou", -14921);

spellPut("pa", -14914);

spellPut("pai", -14908);

spellPut("pan", -14902);

spellPut("pang", -14894);

spellPut("pao", -14889);

spellPut("pei", -14882);

spellPut("pen", -14873);

spellPut("peng", -14871);

spellPut("pi", -14857);

spellPut("pian", -14678);

spellPut("piao", -14674);

spellPut("pie", -14670);

spellPut("pin", -14668);

spellPut("ping", -14663);

spellPut("po", -14654);

spellPut("pu", -14645);

spellPut("qi", -14630);

spellPut("qia", -14594);

spellPut("qian", -14429);

spellPut("qiang", -14407);

spellPut("qiao", -14399);

spellPut("qie", -14384);

spellPut("qin", -14379);

spellPut("qing", -14368);

spellPut("qiong", -14355);

spellPut("qiu", -14353);

spellPut("qu", -14345);

spellPut("quan", -14170);

spellPut("que", -14159);

spellPut("qun", -14151);

spellPut("ran", -14149);

spellPut("rang", -14145);

spellPut("rao", -14140);

spellPut("re", -14137);

spellPut("ren", -14135);

spellPut("reng", -14125);

spellPut("ri", -14123);

spellPut("rong", -14122);

spellPut("rou", -14112);

spellPut("ru", -14109);

spellPut("ruan", -14099);

spellPut("rui", -14097);

spellPut("run", -14094);

spellPut("ruo", -14092);

spellPut("sa", -14090);

spellPut("sai", -14087);

spellPut("san", -14083);

spellPut("sang", -13917);

spellPut("sao", -13914);

spellPut("se", -13910);

spellPut("sen", -13907);

spellPut("seng", -13906);

spellPut("sha", -13905);

spellPut("shai", -13896);

spellPut("shan", -13894);

spellPut("shang", -13878);

spellPut("shao", -13870);

spellPut("she", -13859);

spellPut("shen", -13847);

spellPut("sheng", -13831);

spellPut("shi", -13658);

spellPut("shou", -13611);

spellPut("shu", -13601);

spellPut("shua", -13406);

spellPut("shuai", -13404);

spellPut("shuan", -13400);

spellPut("shuang", -13398);

spellPut("shui", -13395);

spellPut("shun", -13391);

spellPut("shuo", -13387);

spellPut("si", -13383);

spellPut("song", -13367);

spellPut("sou", -13359);

spellPut("su", -13356);

spellPut("suan", -13343);

spellPut("sui", -13340);

spellPut("sun", -13329);

spellPut("suo", -13326);

spellPut("ta", -13318);

spellPut("tai", -13147);

spellPut("tan", -13138);

spellPut("tang", -13120);

spellPut("tao", -13107);

spellPut("te", -13096);

spellPut("teng", -13095);

spellPut("ti", -13091);

spellPut("tian", -13076);

spellPut("tiao", -13068);

spellPut("tie", -13063);

spellPut("ting", -13060);

spellPut("tong", -12888);

spellPut("tou", -12875);

spellPut("tu", -12871);

spellPut("tuan", -12860);

spellPut("tui", -12858);

spellPut("tun", -12852);

spellPut("tuo", -12849);

spellPut("wa", -12838);

spellPut("wai", -12831);

spellPut("wan", -12829);

spellPut("wang", -12812);

spellPut("wei", -12802);

spellPut("wen", -12607);

spellPut("weng", -12597);

spellPut("wo", -12594);

spellPut("wu", -12585);

spellPut("xi", -12556);

spellPut("xia", -12359);

spellPut("xian", -12346);

spellPut("xiang", -12320);

spellPut("xiao", -12300);

spellPut("xie", -12120);

spellPut("xin", -12099);

spellPut("xing", -12089);

spellPut("xiong", -12074);

spellPut("xiu", -12067);

spellPut("xu", -12058);

spellPut("xuan", -12039);

spellPut("xue", -11867);

spellPut("xun", -11861);

spellPut("ya", -11847);

spellPut("yan", -11831);

spellPut("yang", -11798);

spellPut("yao", -11781);

spellPut("ye", -11604);

spellPut("yi", -11589);

spellPut("yin", -11536);

spellPut("ying", -11358);

spellPut("yo", -11340);

spellPut("yong", -11339);

spellPut("you", -11324);

spellPut("yu", -11303);

spellPut("yuan", -11097);

spellPut("yue", -11077);

spellPut("yun", -11067);

spellPut("za", -11055);

spellPut("zai", -11052);

spellPut("zan", -11045);

spellPut("zang", -11041);

spellPut("zao", -11038);

spellPut("ze", -11024);

spellPut("zei", -11020);

spellPut("zen", -11019);

spellPut("zeng", -11018);

spellPut("zha", -11014);

spellPut("zhai", -10838);

spellPut("zhan", -10832);

spellPut("zhang", -10815);

spellPut("zhao", -10800);

spellPut("zhe", -10790);

spellPut("zhen", -10780);

spellPut("zheng", -10764);

spellPut("zhi", -10587);

spellPut("zhong", -10544);

spellPut("zhou", -10533);

spellPut("zhu", -10519);

spellPut("zhua", -10331);

spellPut("zhuai", -10329);

spellPut("zhuan", -10328);

spellPut("zhuang", -10322);

spellPut("zhui", -10315);

spellPut("zhun", -10309);

spellPut("zhuo", -10307);

spellPut("zi", -10296);

spellPut("zong", -10281);

spellPut("zou", -10274);

spellPut("zu", -10270);

spellPut("zuan", -10262);

spellPut("zui", -10260);

spellPut("zun", -10256);

spellPut("zuo", -10254);

}

JAVA实现把汉字转化成拼音???

这个理论上来说是可以的,你可以用一本字典来当数据库,进行比对,来读取汉字的读音然后进行转换。但是做起来未免就很麻烦,当然办法肯定比问题多,希望楼主能找到个好办法,共同分享。

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", "bai", "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", "dao", "de", "deng", "di", "dian", "diao", "die",

"ding", "diu", "dong", "dou", "du", "duan", "dui", "dun", "duo",

"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", "zhi", "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 怎么将拼音转换为汉字

一个汉字 只对应 一个拼音 ,但是一个拼音是对应多个汉字的, 你想要的功能,就好像一个输入法,输入一堆拼音,显示对应的汉字。这个我不知道,有没有输入法提供这样的接口,简单点就是这样,但是复杂点,根据你输入的拼音显示出足够智能的汉字,这不简单。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汉字转拼音代码,java汉字编码转换
地址分享:https://www.cdcxhl.com/article46/hdishg.html

成都网站建设公司_创新互联,为您提供企业建站全网营销推广品牌网站制作软件开发网站设计微信小程序

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都做网站