/**
* 中文读取转换成拼音。
*/import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;import ChineseToSpelling.StringUtil;
import net.sourceforge.pinyin4j.PinyinHelper;
public class ChineseSpellingToPinYin {
/**
* @param args
*/
/**
* 把result字符串中的汉字转化成拼音, 其中第一个字去全拼, 后面的字去第一个拼音。
*/
private static LinkedHashMap nameMap = null;
static {
if (nameMap == null) {
nameMap = new LinkedHashMap(5000);
}
// initialize();
// System.out.println("Chinese transfer Spell Done.");
} /**
* 查找是否存在同样的名字的
*/
public static boolean checkName(String namespell) {
boolean bo = false;
Set keySet = nameMap.keySet();
Iterator it = keySet.iterator();
String key = "";//key
String value = "";// value
while (it.hasNext()) {
key = (String) it.next();// key
Object StrObj = nameMap.get(key);
if (StrObj instanceof String) {
// asciiRang = ((Integer) valObj).intValue();
//
// if (ascii >= asciiRang0 && ascii < asciiRang) { // 区间找到
// return (spell0 == null) ? spell : spell0;
// } else {
// spell0 = spell;
// asciiRang0 = asciiRang;
// }
value = (String) StrObj;// value
//如果输入的姓名拼音(namespell)和nameMap中的value的值相同
if (namespell.equals(value)) {
}
}
System.out.println("key = '" + key + "'\tChinesespelling = '"
+ value + "'");
}
return bo;
} /**
* 把result字符串(汉字)转换为拼音。
*/
public static String SpellingChinese(String result) {
String temp = "";
String temp1 = "";
result = StringUtil.replaceBlank(result);
System.out.print("result = '" + result + "'\t");
// StringBuffer sb = new StringBuffer(temp);
StringBuffer sb = new StringBuffer(); for (int i = 0; i < result.length(); i++) {
char chineseCharacter = result.charAt(i);
temp1 = ChineseSpellingToPinYin
.concatPinyinStringArray(PinyinHelper
.toHanyuPinyinStringArray(chineseCharacter));
// System.out.println("temp1 begin = '"+temp1+"'");
temp1 = temp1.split("\r")[0];
// System.out.println("temp1 end = '"+temp1+"'");
if (temp1.equals(""))
continue;
sb.append(chineseCharacter);
// temp1=StringUtil.replaceBlank(temp1);
// System.out.println(temp1);
if (i != 0) {//不是第一个汉字
// System.out.print("i!=0 temp1 begin= '"+temp1+"'\t"); temp1 = temp1.substring(0, 1);
// System.out.print("temp1 end= '"+temp1+"'\n\n");
} else {
// System.out.print("temp1 begin= '"+temp1+"'\t");
int n = temp1.length();
temp1 = temp1.substring(0, n - 1);
// System.out.print("n = '"+n+"' temp1 end= '"+temp1+"'\n");
}
temp = temp + temp1;
}
temp = temp.toUpperCase();// 转换为大写
if (temp.indexOf("U:") != -1) {
temp = temp.replaceAll("U:", "V");
System.out.println(temp + "\n");
}
// System.out.println("result = '"+temp.toUpperCase()+"'");
nameMap.put(sb.toString(), temp);
return temp;
} /**
* 得到汉字的所有的拼音,包括多音。
*/
public static String concatPinyinStringArray(String[] pinyinArray) {
StringBuffer pinyinStrBuf = new StringBuffer();
if ((null != pinyinArray) && (pinyinArray.length > 0)) {
for (int i = 0; i < pinyinArray.length; i++) {
pinyinStrBuf.append(pinyinArray[i]);
pinyinStrBuf.append(System.getProperty("line.separator"));
}
}
String outputString = pinyinStrBuf.toString();
return outputString;
} public static void main(String[] args) {
// ChineseSpellingToPinYin.checkName();
System.out.println(ChineseSpellingToPinYin.SpellingChinese("张三"));
}
}
最后的输出结果为:Zhangs 姓名的全拼+名字的第一个字母
* 中文读取转换成拼音。
*/import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;import ChineseToSpelling.StringUtil;
import net.sourceforge.pinyin4j.PinyinHelper;
public class ChineseSpellingToPinYin {
/**
* @param args
*/
/**
* 把result字符串中的汉字转化成拼音, 其中第一个字去全拼, 后面的字去第一个拼音。
*/
private static LinkedHashMap nameMap = null;
static {
if (nameMap == null) {
nameMap = new LinkedHashMap(5000);
}
// initialize();
// System.out.println("Chinese transfer Spell Done.");
} /**
* 查找是否存在同样的名字的
*/
public static boolean checkName(String namespell) {
boolean bo = false;
Set keySet = nameMap.keySet();
Iterator it = keySet.iterator();
String key = "";//key
String value = "";// value
while (it.hasNext()) {
key = (String) it.next();// key
Object StrObj = nameMap.get(key);
if (StrObj instanceof String) {
// asciiRang = ((Integer) valObj).intValue();
//
// if (ascii >= asciiRang0 && ascii < asciiRang) { // 区间找到
// return (spell0 == null) ? spell : spell0;
// } else {
// spell0 = spell;
// asciiRang0 = asciiRang;
// }
value = (String) StrObj;// value
//如果输入的姓名拼音(namespell)和nameMap中的value的值相同
if (namespell.equals(value)) {
}
}
System.out.println("key = '" + key + "'\tChinesespelling = '"
+ value + "'");
}
return bo;
} /**
* 把result字符串(汉字)转换为拼音。
*/
public static String SpellingChinese(String result) {
String temp = "";
String temp1 = "";
result = StringUtil.replaceBlank(result);
System.out.print("result = '" + result + "'\t");
// StringBuffer sb = new StringBuffer(temp);
StringBuffer sb = new StringBuffer(); for (int i = 0; i < result.length(); i++) {
char chineseCharacter = result.charAt(i);
temp1 = ChineseSpellingToPinYin
.concatPinyinStringArray(PinyinHelper
.toHanyuPinyinStringArray(chineseCharacter));
// System.out.println("temp1 begin = '"+temp1+"'");
temp1 = temp1.split("\r")[0];
// System.out.println("temp1 end = '"+temp1+"'");
if (temp1.equals(""))
continue;
sb.append(chineseCharacter);
// temp1=StringUtil.replaceBlank(temp1);
// System.out.println(temp1);
if (i != 0) {//不是第一个汉字
// System.out.print("i!=0 temp1 begin= '"+temp1+"'\t"); temp1 = temp1.substring(0, 1);
// System.out.print("temp1 end= '"+temp1+"'\n\n");
} else {
// System.out.print("temp1 begin= '"+temp1+"'\t");
int n = temp1.length();
temp1 = temp1.substring(0, n - 1);
// System.out.print("n = '"+n+"' temp1 end= '"+temp1+"'\n");
}
temp = temp + temp1;
}
temp = temp.toUpperCase();// 转换为大写
if (temp.indexOf("U:") != -1) {
temp = temp.replaceAll("U:", "V");
System.out.println(temp + "\n");
}
// System.out.println("result = '"+temp.toUpperCase()+"'");
nameMap.put(sb.toString(), temp);
return temp;
} /**
* 得到汉字的所有的拼音,包括多音。
*/
public static String concatPinyinStringArray(String[] pinyinArray) {
StringBuffer pinyinStrBuf = new StringBuffer();
if ((null != pinyinArray) && (pinyinArray.length > 0)) {
for (int i = 0; i < pinyinArray.length; i++) {
pinyinStrBuf.append(pinyinArray[i]);
pinyinStrBuf.append(System.getProperty("line.separator"));
}
}
String outputString = pinyinStrBuf.toString();
return outputString;
} public static void main(String[] args) {
// ChineseSpellingToPinYin.checkName();
System.out.println(ChineseSpellingToPinYin.SpellingChinese("张三"));
}
}
最后的输出结果为:Zhangs 姓名的全拼+名字的第一个字母
解决方案 »
- Struts2.0和sping3.0结合 诡异异常Unable to instantiate Action
- input输入框的入力值得限制
- apache-tomcat-6.0.26 eclipse console字体为红色
- 困扰多时的问题,请各位大侠指点啊~~jsp直接下载!!
- 关于JS访问JSP页面中java数据的问题。
- 地址栏输入Resource.do ,为何不先进Resource.action
- 一个关于数据库技术的好网站
- hibernate3.1 怎么操作 blob类型啊
- 过滤器解决中文乱码时遇到奇怪的问题
- springmvc return跳转的一个小问题。
- 一个代码main方法和action中执行的却是两种结果
- 数据库中字段匹配问题,求助!
楼主【yansongyang】截止到2008-07-08 11:45:15的历史汇总数据(不包括此帖):
发帖的总数量:8 发帖的总分数:55 每贴平均分数:6
回帖的总数量:31 得分贴总数量:2 回帖的得分率:6%
结贴的总数量:2 结贴的总分数:10
无满意结贴数:1 无满意结贴分:20
未结的帖子数:6 未结的总分数:45
结贴的百分比:25.00 % 结分的百分比:18.18 %
无满意结贴率:50.00 % 无满意结分率:200.00%
楼主该结一些帖子了