public static String getFirstSpell(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.substring(0, 1)); } // end of if spell == null } // end of if ascii <= -20400 } // end of for return retuBuf.toString().toUpperCase(); }
楼主【wanderung】截止到2008-07-03 15:04:47的历史汇总数据(不包括此帖):
发帖的总数量:3 发帖的总分数:300
结贴的总数量:2 结贴的总分数:200
无满意结贴数:0 无满意结贴分:0
未结的帖子数:1 未结的总分数:100
结贴的百分比:66.67 % 结分的百分比:66.67 %
无满意结贴率:0.00 % 无满意结分率:0.00 %
楼主加油
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;public class HzSpell
{
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));
}
{
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 getFirstSpell(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.substring(0, 1));
} // end of if spell == null
} // end of if ascii <= -20400
} // end of for
return retuBuf.toString().toUpperCase();
}
}
http://blog.csdn.net/lip009/archive/2006/10/13/1333723.aspx