java 获取文字首写字母
来源:互联网 发布:网络电视打开u盘没内容 编辑:程序博客网 时间:2024/05/18 02:34
package com.imcc.breakdown.action.sign;public class Sign {//字母Z使用了两个标签,这里有27个值 //i, u, v都不做声母, 跟随前面的字母 private char[] chartable = { '啊', '芭', '擦', '搭', '蛾', '发', '噶', '哈', '哈', '击', '喀', '垃', '妈', '拿', '哦', '啪', '期', '然', '撒', '塌', '塌', '塌', '挖', '昔', '压', '匝', '座' }; private static char[] alphatable = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; private static int[] table = new int[27]; //初始化 { for (int i = 0; i < 27; ++i) { table[i] = gbValue(chartable[i]); } } //主函数,输入字符,得到他的声母, //英文字母返回对应的大写字母 //其他非简体汉字返回 '0' public static char Char2Alpha(char ch) { if (ch >= 'a' && ch <= 'z') return (char) (ch - 'a' + 'A'); if (ch >= 'A' && ch <= 'Z') return ch; int gb = gbValue(ch); if (gb < table[0]) return '0'; int i; for (i = 0; i < 26; ++i) { if (match(i, gb)) break; } if (i >= 26) return '0'; else return alphatable[i]; } //根据一个包含汉字的字符串返回一个汉字拼音首字母的字符串 public static String String2Alpha(String SourceStr){ String Result = ""; int StrLength = 1; int i; try { for (i = 0; i < StrLength; i++) { Result += Char2Alpha(SourceStr.charAt(i)); } } catch (Exception e) { Result = ""; } return Result; } private static boolean match(int i, int gb) { if (gb < table[i]) return false; int j = i + 1; //字母Z使用了两个标签 while (j < 26 && (table[j] == table[i])) ++j; if (j == 26) return gb <= table[j]; else return gb < table[j]; } //取出汉字的编码 private static int gbValue(char ch) { String str = new String(); str += ch; try { byte[] bytes = str.getBytes("GB2312"); if (bytes.length < 2) return 0; return (bytes[0] << 8 & 0xff00) + (bytes[1] & 0xff); } catch (Exception e) { return 0; } } public static void main(String[] args) { Sign obj1 = new Sign(); System.out.println(obj1.String2Alpha("奔驰")); System.out.println(obj1.String2Alpha("宝马")); return; }}
0 0
- java 获取文字首写字母
- 汉字首写字母获取算法
- Python 获取中文字拼音首个字母
- C#根据文字获取拼音、首字字母
- Android Button设置文字 字母自动变大写...
- question_006-JAVA之获取中文字母的首地址???
- excel生成首写字母
- java 图片上写文字
- HDU_2026 首写字母变大写
- hduoj 2026 首写字母变大写
- 把首写字母大写,其他的字母小写
- java获取随机字母数字组合
- java获取字母出现的次数
- java获取随机字母数字组合
- java获取随机字母数字组合
- 根据当前字母获取下一位 java
- mysql 中文首写字母排序
- 中文转化成拼音首写字母
- js禁止页面回退,刷新,右键代码
- event.currentTarget与event.target的区别介绍
- STM32下SD卡驱动详解
- POC(proof-of-concept)
- 安卓uiautomator使用步骤
- java 获取文字首写字母
- 成为一个优秀的嵌入式工程师所需技能
- 如何使用 Docker 组件开发 Django 项目?
- Objective-C 编程艺术
- 素数打表
- laplacian算子的运用
- 利用getViewTreeObserver().addOnGlobalLayoutListener()获得一个视图的高度
- 观察者模式实例解析
- perl函数