java取得中文的首字母
来源:互联网 发布:微信淘宝返利源码 编辑:程序博客网 时间:2024/05/11 04:41
取得中文字符的首字母。
public class CharAbbreviate ...{
private static final String _FromEncode_ = "GBK";
private static final String _ToEncode_ = "GBK";
/** *//**
* 是否过滤特殊字符
* */
private static boolean isOutSpecialCharacter = false;
public CharAbbreviate() ...{
}
public static int compare(String str1, String str2)
...{
int result = 0;
String m_s1 = null;
String m_s2 = null;
try
...{
m_s1 = new String(str1.getBytes(_FromEncode_), _ToEncode_);
m_s2 = new String(str2.getBytes(_FromEncode_), _ToEncode_);
}
catch(Exception e)
...{
return str1.compareTo(str2);
}
result = chineseCompareTo(m_s1, m_s2);
return result;
}
public static int getCharCode(String s)
...{
if(s == null && s.equals(""))
return -1;
byte b[] = s.getBytes();
int value = 0;
for(int i = 0; i < b.length && i <= 2; i++)
value = value * 100 + b[i];
return value;
}
public static int chineseCompareTo(String s1, String s2)
...{
int len1 = s1.length();
int len2 = s2.length();
int n = Math.min(len1, len2);
for(int i = 0; i < n; i++)
...{
int s1_code = getCharCode(s1.charAt(i) + "");
int s2_code = getCharCode(s2.charAt(i) + "");
if(s1_code * s2_code < 0)
return Math.min(s1_code, s2_code);
if(s1_code != s2_code)
return s1_code - s2_code;
}
return len1 - len2;
}
public static String getBeginCharacter(String res)
...{
String a = res;
String result = "";
for(int i = 0; i < a.length(); i++)
...{
String current = a.substring(i, i + 1);
if(compare(current, "啊") < 0)
result = result + current;
else
if(compare(current, "啊") >= 0 && compare(current, "座") <= 0)
if(compare(current, "匝") >= 0)
result = result + "z";
else
if(compare(current, "压") >= 0)
result = result + "y";
else
if(compare(current, "昔") >= 0)
result = result + "x";
else
if(compare(current, "挖") >= 0)
result = result + "w";
else
if(compare(current, "塌") >= 0)
result = result + "t";
else
if(compare(current, "撒") >= 0)
result = result + "s";
else
if(compare(current, "然") >= 0)
result = result + "r";
else
if(compare(current, "期") >= 0)
result = result + "q";
else
if(compare(current, "啪") >= 0)
result = result + "p";
else
if(compare(current, "哦") >= 0)
result = result + "o";
else
if(compare(current, "拿") >= 0)
result = result + "n";
else
if(compare(current, "妈") >= 0)
result = result + "m";
else
if(compare(current, "垃") >= 0)
result = result + "l";
else
if(compare(current, "喀") >= 0)
result = result + "k";
else
if(compare(current, "击") > 0)
result = result + "j";
else
if(compare(current, "哈") >= 0)
result = result + "h";
else
if(compare(current, "噶") >= 0)
result = result + "g";
else
if(compare(current, "发") >= 0)
result = result + "f";
else
if(compare(current, "蛾") >= 0)
result = result + "e";
else
if(compare(current, "搭") >= 0)
result = result + "d";
else
if(compare(current, "擦") >= 0)
result = result + "c";
else
if(compare(current, "芭") >= 0)
result = result + "b";
else
if(compare(current, "啊") >= 0)
result = result + "a";
}
return result;
}
/** *//**取得中文字符的首字母
* @param chineseStr 中文字符
* @return 中文字符串的首字母
* @author
*/
public static String getFirstStr(String str)
...{
char a = str.charAt(0);
char aa[] = ...{
a
};
String sss = new String(aa);
if(Character.isDigit(aa[0]))...{
//如果输入了数字将数字返回
sss = String.valueOf(aa[0]);
}else
if(a >= 'a' && a <= 'z' || a >= 'A' && a <= 'Z')...{
//如果输入了字母,将原字母返回去
sss = String.valueOf(a);
}else if ( (a >= '[' && a <= ']') || a == ',' || a == '.' || a == '/'||
a == '*' || (a == '@') || (a >= '{' && a <= '~') || a == '》' ||
a == '《' || a == ';' || a == '%' || a == '&' || a == '#' ||
a == '_' || a == '^' || a == '(' || a == ')' || a == '=' ||
a == '+' || a == '-' || a == '`' || a == '$' || a == '“' ||
a == '|' || a == ''') ...{
if ( isOutSpecialCharacter )
sss = String.valueOf(a);
else
return "";
} else ...{
sss = getBeginCharacter(sss);
}
return sss.toLowerCase();
}
/** *//**取得中文字符串的首字母
* @param chineseStr 中文字符串
* @return 中文字符串的首字母
* @author
*/
public static String getAbbreviate(String chineseStr)...{
if (chineseStr==null) ...{
return "";
}
StringBuffer buffer =new StringBuffer();
int length = chineseStr.length();
for (int i=0;i<length;i++) ...{
buffer.append(getFirstStr(chineseStr.substring(i,i+1)));
}
return buffer.toString().toLowerCase();
}
/** *//**取得中文字符串的首字母
* @param chineseStr 中文字符串
* @return 中文字符串的首字母
* @author
*/
public static String getAbbreviate(String chineseStr,boolean outSpecialCharacter )...{
if (chineseStr==null) ...{
return "";
}
isOutSpecialCharacter = outSpecialCharacter;
StringBuffer buffer =new StringBuffer();
int length = chineseStr.length();
for (int i=0;i<length;i++) ...{
buffer.append(getFirstStr(chineseStr.substring(i,i+1)));
}
return buffer.toString().toLowerCase();
}
}
- java取得中文的首字母
- 使用Java取得汉字的拼音首字母
- 使用Java取得汉字的拼音首字母
- 取得汉字的首字母
- 使用Java取得汉字的拼音首字母(转)
- 使用Java取得汉字的拼音首字母(转)
- 取得汉字的拼音首字母
- java实现中文汉字的首字母排序
- java实现中文汉字的首字母排序
- JAVA获取中文汉字的拼音首字母
- JAVA获取中文名字的首字母
- java实现中文汉字的首字母排序
- JAVA获取中文名字的首字母
- JAVA获取中文名字的首字母
- java实现中文汉字的首字母排序
- java实现中文汉字的首字母排序
- java实现提取中文的首字母
- java实现中文汉字的首字母排序
- 函数调用规范__cdecl和__stdcall的区别
- 验证码识别技术 http://www.yhhe.net/ape/book/fap/f2/ix.html
- Linux操作系统下串口编程
- 浅谈图形验证码的识别! http://www.g168.net/html/net_008/2007/1217/20071436236972.html
- 闲谈项目管理之人员管理篇
- java取得中文的首字母
- test命令
- 计算机基础知识试题doc
- 显示隐藏文件全攻略
- 在安装程序中使用.bat文件
- 项目外包中如何敏捷开发,节约成本!
- 发送附件时,防止文件名中的中文字符变成乱码
- 我整理的Ruby on Rails教程 和Ruby&Rails 入门大全,对新手很有用
- The early bird catches the worm!