Java字符串转化问题:
楼上的 楼主敏简或是要求输入字母求得相咐消关的汉字字符串,而你音品码查询是从汉字求得首字母吧?
我做过一个公交查询系统,其中有个功能就是通过字母显示出相应的站点。
首先,你想通过‘W’得到王力宏、王菲等你就必须先把这些名字存到数据库中吧,然后你再在数据库中给这些名字项添加一个首字母的字段,当然你不用手动去输入它们的首字母,写个方法循环把它们的首字母输出并存入数据库中,方法可用楼上的音品码查询,我也写了个类似的方法,如下:
public class StringUtil {
//private static Log logger = LogFactory.getLog(StringUtil.class);
// 国标码和区位码转换常量
int GB_SP_DIFF = 160;
//存放国标一级汉字不同读音的起始区位码
int[] secPosValueList = {
1601, 1637, 1833, 2078, 2274, 2302, 2433, 2594, 2787,
3106, 3212, 3472, 3635, 3722, 3730, 3858, 4027, 4086,
4390, 4558, 4684, 4925, 5249, 5600};
//存放国标一级汉字不同读音的起始区位码对应读音
char[] firstLetter = {
‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘j’,
‘k’, ‘l’, ‘m’桥伍, ‘n’, ‘o’, ‘p’, ‘q’, ‘r’, ‘s’,
‘t’, ‘w’, ‘x’, ‘y’, ‘z’};
char convert(String ch) {
byte[] bytes=new byte[2];
try {
bytes = ch.getBytes(“GB2312”);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
char result = ‘-‘;
int secPosValue = 0;
int i;
for (i = 0; i bytes.length; i++) {
bytes[i] -= GB_SP_DIFF;
}
secPosValue = bytes[0] * 100 + bytes[1];
for (i = 0; i 23; i++) {
if (secPosValue = secPosValueList[i] secPosValue secPosValueList[i + 1]) {
result = firstLetter[i];
break;
}
}
return result;
}
}
调用convert(String str)方法就是返回str字符串的首字的首字母。其它的应该没什么难的了
java代码如何实现中英文转换
使用java提供的国穗郑际化功能猜答颂就可以了。不过建议你使用框架技术中的国际化,框架技术中的国际化都做过封装,实现起来相对简单的多。只需要简单的配举埋置就可以实现中英文或者其他语言的切换了!~
java如何将数字转换为英文
Java 数字转成英文
英文数词,按3位3位区分
Hundred: 100
Thousand:1,000
Million: 1,000,000
Billion: 1,000,000,000
Trillion: 1,000,000,000,000
Quintillion: 1,000,000,000,000,000,000
Sextillion: 1,000,000,000,000,000,000,000
Nonillion: 1,000,000,000,000,000,000,000,000,000,000
Centillion: 1 followed by 303 zeros
所以把数字字符串按3位分割,分别解析
public class NumUtil {
public static final String[] enNum = { // 基本数词表
“zero”, “one”, “tow”, “three”, “four”, “five”, “six”, “seven”, “eight”,
“nine”, “ten”, “eleven”, “twelve”, “thirteen”, “fourteen”,
“fifteen”, “sixteen”, “seventeen”, “eighteen”, “nineteen”,
“twenty”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “thirty”, “”, “”, “帆埋巧”,
“”, “”, “”, “”, “”, “”, “fourty”, “”, “”, “”, “”, “”, “”, “”, “”,
“”, “fifty”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “sixty”, “”, “”,
“”, “”, “”, “”, “”, “”, “”, “seventy”, “”, “”, “”, “”, “”, “”, “”,
“”, “”, “态键eighty”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “ninety” };
public static final String[] enUnit = { “hundred”, “thousand”, “million”,
“billion”, “trillion”, “quintillion” }; // 单位表
public static void main(String[] args) {
System.out.println(analyze(1)); // 测试数据
System.out.println(analyze(21));
System.out.println(analyze(105));
System.out.println(analyze(3250));
System.out.println(analyze(47826));
System.out.println(analyze(56945781));
}
public static String analyze(long num) { // long型参数,
return analyze(String.valueOf(num)); // 因为long型有极限,所以以字符串参数方法为主
}
public static String analyze(String num) { // 数字字符串参数
// 判断字符串是否为数字
if (!num.matches(“\\d+”)) {
return String.format(“%s is not number”, num);
}
num = num.replaceAll(“^[0]*([1-9]*)”, “$1”); // 把字符串前面的0去掉
if (num.length() == 0) { // 如果长度为0,则原串都是0
return enNum[0];
} else if (num.length() 9) { // 如果大于9,即大于999999999,题目限制条件
return “too big”;
}
// 按3位分割分组
int count = (num.length() % 3 == 0) ? num.length() / 3
: num.length() / 3 + 1;
if (count enUnit.length) {
return “too big”;
} // 判断组液源单位是否超过,
// 可以根据需求适当追加enUnit
String[] group = new String[count];
for (int i = num.length(), j = group.length – 1; i 0; i -= 3) {
group[j–] = num.substring(Math.max(i – 3, 0), i);
}
StringBuilder buf = new StringBuilder(); // 结果保存
for (int i = 0; i count; i++) { // 遍历分割的组
int v = Integer.valueOf(group[i]);
if (v = 100) { // 因为按3位分割,所以这里不会有超过999的数
buf.append(enNum[v / 100]).append(” “).append(enUnit[0])
.append(” “);
v = v % 100; // 获取百位,并得到百位以后的数
if (v != 0) {
buf.append(“and “);
} // 如果百位后的数不为0,则追加and
}
if (v != 0) { // 前提是v不为0才作解析
if (v 20 || v % 10 == 0) { // 如果小于20或10的整数倍,直接取基本数词表的单词
buf.append(enNum[v]).append(” “);
} else { // 否则取10位数词,再取个位数词
buf.append(enNum[v – v % 10]).append(” “);
buf.append(enNum[v % 10]).append(” “);
}
if (i != count – 1) { // 百位以上的组追加相应的单位
buf.append(enUnit[count – 1 – i]).append(” “);
}
}
}
return buf.toString().trim(); // 返回值
}
}