`

汉字转全拼和首字母-转-已测试

阅读更多

import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;

public class SpellUtils
{
public static String getSpell(String c){
   
     char[] chars = c.toCharArray();
   
     StringBuffer sb = new StringBuffer("");
   
     for(int i=0;i<chars.length;i++){
      sb.append(getSpell(chars[i]));
     }
   
     return sb.toString();
  }


  public static String getSpell(char c)
      {
  int b = (int) c;
  if (b>=48 && b<=57) return String.valueOf(c);
  if (b>=65 && b<=90) return String.valueOf(c);
  if (b>=97 && b<=122) return String.valueOf(c);
 
              byte[] array = new byte[2];
              try {
      array = String.valueOf(c).getBytes("gbk");
     } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
     }
              if(array.length<2)return String.valueOf(c);
              int i = (short)(array[0] - '\0' + 256) * 256 + ((short)(array[1] - '\0' + 256));
              if ( i < 0xB0A1) return "";//String.valueOf(c);
              if ( i < 0xB0C5) return "a";
              if ( i < 0xB2C1) return "b";
              if ( i < 0xB4EE) return "c";
              if ( i < 0xB6EA) return "d";
              if ( i < 0xB7A2) return "e";
              if ( i < 0xB8C1) return "f";
              if ( i < 0xB9FE) return "g";
              if ( i < 0xBBF7) return "h";
              if ( i < 0xBFA6) return "j";
              if ( i < 0xC0AC) return "k";
              if ( i < 0xC2E8) return "l";
              if ( i < 0xC4C3) return "m";
              if ( i < 0xC5B6) return "n";
              if ( i < 0xC5BE) return "o";
              if ( i < 0xC6DA) return "p";
              if ( i < 0xC8BB) return "q";
              if ( i < 0xC8F6) return "r";
              if ( i < 0xCBFA) return "s";
              if ( i < 0xCDDA) return "t";
              if ( i < 0xCEF4) return "w";
              if ( i < 0xD1B9) return "x";
              if ( i < 0xD4D1) return "y";
              if ( i < 0xD7FA) return "z";
              return String.valueOf(c);
      }
 
  private static LinkedHashMap<String, Integer> spellMap = null;
static
{
if (spellMap == null)
{
spellMap = new LinkedHashMap<String, Integer>(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);
}

private static int getCnAscii(char cn)
{
byte[] bytes = null;
try
{
bytes = (String.valueOf(cn)).getBytes("GBK");
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
if (bytes == null || bytes.length > 2 || bytes.length <= 0)
{
return 0;
}
if (bytes.length == 1)
{
return bytes[0];
}
if (bytes.length == 2)
{
int hightByte = 256 + bytes[0];
int lowByte = 256 + bytes[1];
int ascii = (256 * hightByte + lowByte) - 256 * 256;
return ascii;
}
return 0;
}

private static String getSpellByAscii(int ascii)
{
if (ascii > 0 && ascii < 160)
{
return String.valueOf((char) ascii);
}
if (ascii < -20319 || ascii > -10247)
{
return null;
}
Set<String> keySet = spellMap.keySet();
Iterator<String> it = keySet.iterator();
String spell0 = null;
String spell = null;

int asciiRang0 = -20319;
int asciiRang;
while (it.hasNext())
{
spell = (String) it.next();
Object valObj = spellMap.get(spell);
if (valObj instanceof Integer)
{
asciiRang = ((Integer) valObj).intValue();
if (ascii >= asciiRang0 && ascii < asciiRang)
{
return (spell0 == null) ? spell : spell0;
}
else
{
spell0 = spell;
asciiRang0 = asciiRang;
}
}
}
return null;
}

private static boolean validate(String cnStr)
{
if (cnStr == null || cnStr.trim().equals(""))
{
return false;
}
else
{
return true;
}
}

public static String getFullSpell(String cnStr)
{
if (!validate(cnStr))
{
return cnStr;
}
char[] chars = cnStr.toCharArray();
StringBuffer retuBuf = new StringBuffer();
for (int i = 0, Len = chars.length; i < Len; i++)
{
int ascii = getCnAscii(chars[i]);
if (ascii == 0)
{ // ȡasciiʱ���
retuBuf.append(chars[i]);
}
else
{
String spell = getSpellByAscii(ascii);
if (spell == null)
{
retuBuf.append(chars[i]);
}
else
{
retuBuf.append(spell);
} // end of if spell == null
} // end of if ascii <= -20400
} // end of for
return retuBuf.toString();
}
 
  public static void main(String[] args){
System.out.println(getSpell("中国人"));
System.out.println(getFullSpell("中国人"));
  }

}

//结果
zgr
zhongguoren

分享到:
评论

相关推荐

    汉字转拼音声母首字母 dll

    标题中的“汉字转拼音声母首字母 dll”指的是一个动态链接库(DLL)文件,它主要用于将汉字转换为对应的拼音声母首字母。在计算机编程中,DLL是一种可被多个程序共享的代码库,可以提高资源利用率并降低内存占用。...

    DelPhi中文转拼音,全拼和首字母

    标题中的"Delphi中文转拼音,全拼和首字母"是指使用Delphi编程语言开发的一个功能,该功能能够将输入的中文文本转化为对应的拼音表示,包括全拼(每个汉字转化为完整的拼音)和首字母(每个汉字转化为声母,即汉语...

    SQL 汉字转拼音首字母

    ### SQL汉字转拼音首字母知识点解析 #### 一、背景介绍 在数据库处理过程中,有时候我们需要将汉字转换为对应的拼音,特别是在中文环境下的数据检索、排序等操作中,这种需求尤为常见。本篇文章将深入探讨如何利用...

    中文汉字转拼音 大写(默认) 小写 首字母大写

    3. **首字母大写**:这种形式是每个汉字拼音的首字母采用大写,其余字母小写。例如,“北京”会转换为“Běi Jīng”。这种格式常用于创建缩写或者在标题、标题句等需要强调的文本中。 汉字转拼音的实现通常依赖于...

    查询汉字首字母及全拼sql自定义方法

    在提供的压缩包文件中,有两个SQL文件:`汉字全拼.sql`和`汉字首字母.sql`。这两个文件很可能分别包含了实现汉字全拼查询和首字母查询的SQL脚本。`汉字全拼.sql`可能包含了一个函数或视图,用于获取汉字的完整拼音,...

    delphi汉字转全拼或简拼代码

    "delphi汉字转全拼或简拼代码"的标题指出,这是一段Delphi程序,能够实现将汉字转化为拼音全拼或拼音首字母的功能。这样的功能对于处理中文字符集,尤其是进行信息检索和数据分析时非常有用。 描述中提到,这段代码...

    数据库获取中文首字母自定义函数(字母查汉字模糊查询)

    标题“数据库获取中文首字母自定义函数(字母查汉字模糊查询)”指的是在数据库系统中定义一个函数,该函数能接受用户输入的英文首字母,然后返回所有以该首字母为拼音首字母的中文字段。这种功能对于具有大量中文...

    IOS通讯录T9键盘全拼、首字母搜索实现

    总的来说,实现iOS通讯录的T9键盘全拼和首字母搜索功能需要深入理解字符串处理、数据结构和算法,以及良好的用户交互设计。这一功能的实现不仅能提升用户对应用的满意度,也能展现开发者在iOS开发领域的专业素养。

    js汉字转拼音工具

    这个工具通过JavaScript编程语言实现了将汉字转换为对应的拼音,提供了三种主要功能:获取汉字的首字母大写拼音,获取汉字的大写全拼,以及获取汉字的小写全拼。 首先,让我们了解一下汉字转拼音的基本原理。在...

    安卓A-Z字母排序索引相关-匹配中文字符串的拼音首字母或英文字符串的首字母缩写.rar

    总的来说,这个压缩包提供了匹配中文字符串拼音首字母和英文字符串首字母的功能实现,对于初学者来说,是学习和理解A-Z字母排序索引原理的好材料。在实际应用中,开发者需要结合这些知识点,根据具体需求进行定制和...

    C#编写的Winform工具: 1.密码生成器;2.GUID生成器, 中文拼音首字母/全拼-转换(源码+exe导出软件双击即可)

    介绍 C#编写的Winform应用程序,开发...中文拼音首字母/全拼-转换器 == 2024-6-19 增加“身份证”“测试数据-人员姓名”生成功能 增加“md5”加密串功能 日志 2023.11.10 提取首字母,或拼音,提供“大小写”选择功能

    java获取汉字的首字母

    一个常用的库是`Pinyin4j`,它是一个用于Java的中文拼音处理库,可以方便地获取汉字的全拼和首字母。 以下是一个使用`Pinyin4j`库获取汉字首字母的基本步骤: 1. **添加依赖**: 在项目中引入`Pinyin4j`库,如果是...

    汉字转拼音

    // ----------------------------------------返回中文的首字母-------------------- public static String getPinYinHeadChar(String str) { String convert = ""; for (int j = 0; j (); j++) { char word ...

    提供汉字、拼音、拼音首字母的模糊查询(匹配左侧)

    - 清空当前列表框中的内容,然后根据用户输入的查询条件,遍历所有数据,对每条数据进行上述的汉字、拼音和拼音首字母的匹配。 - 匹配到的结果添加到列表框中,按匹配度排序,以便用户可以直观地看到最相关的项在...

    汉字转换拼音 java

    在IT行业中,尤其是在文本处理和自然语言处理领域,汉字转换...Pinyin4j是一个常用的库,它提供了丰富的功能,包括获取拼音全拼、首字母等。结合提供的代码示例和压缩包中的资源,你可以快速上手并应用到自己的项目中。

    获取汉字拼音首字母

    在IT领域,尤其是在文本处理和信息检索中,获取汉字拼音首字母是一项常见的需求。这个功能可以帮助我们快速地对汉字进行编码,便于计算机处理和搜索。例如,在开发搜索引擎、拼音输入法或者建立汉字索引时,将汉字...

    Unicode汉字拼音首字母查询

    在IT行业中,Unicode汉字拼音首字母查询是一个常见的需求,尤其在文本处理、搜索引擎优化(SEO)和信息检索等领域。这个工具或程序允许开发者快速获取一个汉字对应的拼音首字母,这对于处理大量汉字数据时非常有用。...

    汉字转换拼音首拼及五笔字母等

    在这个压缩包中,"拼音首拼"指的是汉字拼音的首字母缩写,例如“中国”转换为“zhg”,而“全拼”则是完整的拼音,如“中国”转换为“zhongguo”。 Delphi是Borland公司开发的一种面向对象的 Pascal 编程语言,常...

    生成拼音首字符及全拼单元。根据网上代码,测试、整理,真实项目应用。有测试例子。

    这个压缩包包含的资源显然是一套用以生成汉字拼音首字母和全拼的程序,适用于真实的项目应用,并且提供了测试案例以确保其功能的正确性。 首先,我们要了解这个程序的核心功能。它能够将汉字转化为拼音,这包括获取...

    汉字返回拼音首字母 asp.net代码

    一种常见的实现方式是使用第三方库,如Pinyin4NET,这是一个.NET平台下的汉字拼音处理库,它可以将汉字转换为全拼和首字母。首先,需要将这个库添加到项目中,可以通过NuGet包管理器安装。然后,在代码中引用库,并...

Global site tag (gtag.js) - Google Analytics