您现在的位置: 无忧电子商务网 >> 信息学院 >> 程序开发 >> js >> 正文

用Java转化汉字为拼音全拼

作者:作者:未…    信息学院来源:网络收集    点击数:    更新时间:2006-8-27 我要参与讨论

  n bytes[0];
}
if(bytes.length == 2){ //中文字符
int hightByte = 256 + bytes[0];
int lowByte = 256 + bytes[1];

int ascii = (256 * hightByte + lowByte) - 256 * 256;

//System.out.println("ASCII=" + ascii);

return ascii;
}

return 0; //错误
}

/**
* 根据ASCII码到SpellMap中查找对应的拼音
* @param ascii int
* 字符对应的ASCII
* @return String
* 拼音,首先判断ASCII是否>0&<160,如果是返回对应的字符,
* <BR>否则到SpellMap中查找,如果没有找到拼音,则返回null,如果找到则返回拼音.
*/
public static String getSpellByAscii(int ascii)
{
if(ascii > 0 && ascii < 160){ //单字符
return String.valueOf((char)ascii);
}

if(ascii < -20319 || ascii > -10247){ //不知道的字符
return null;
}

Set keySet = spellMap.keySet();
Iterator 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;

}

/**
* 返回字符串的全拼,是汉字转化为全拼,其它字符不进行转换
* @param cnStr String
* 字符串
* @return String
* 转换成全拼后的字符串
*/
public static String getFullSpell(String cnStr)
{
if(null == cnStr || "".equals(cnStr.trim())){
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 String getFirstSpell(String cnStr)
{
return null;
}

public static void main(String[] args)
{
String str = null;
str = "谢海101普降喜雨";
System.out.println("Spell=" + CnToSpell.getFullSpell(str));

str = "张牙舞爪》。,";
System.out.println("Spell=" + CnToSpell.getFullSpell(str));

str = "李鹏,可耻下场。";
System.out.println("Spell=" + CnToSpell.getFullSpell(str));

上一页  [1] [2] [3] [4] [5] [6] [7] [8] 下一页

在google里搜索更多用Java转化汉字为拼音全拼

Google
Web www.51ec.org
【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
我来说两句 对此文章发表了评论
  昵 称: *必填    ·注册用户·
  评 分: 1分 2分 3分 4分 5分     严禁发表危害国家安全、政治、黄色淫秽等内容的评论,用户需对自己在使用本网站服务过程中的行为承担法律责任。本站管理员有权保留或删除评论内容,评论内容只代表机友个人观点,与本网站立场无关。  
评 论
内 容

 
评论列表 (最新 评论仅限网友观点!)

推荐文章
  • 此栏目下没有推荐信息学院
  • 供求信息




    | 设为首页 | 加入收藏 | 关于我们 | 广告服务 | 联系方式 | 友情链接 | 版权申明