From f763655151f9b464e146a79f247bec658dff75e6 Mon Sep 17 00:00:00 2001 From: Blankj <625783482@qq.com> Date: Wed, 16 Nov 2016 22:35:49 +0800 Subject: [PATCH] see 11/16 log --- README-CN.md | 54 +++-- README.md | 24 +- .../activities/LocationActivity.java | 32 +++ app/src/main/res/layout/activity_location.xml | 23 ++ update_log.md | 1 + .../com/blankj/utilcode/utils/ConstUtils.java | 6 +- .../blankj/utilcode/utils/LocationUtils.java | 173 ++++++++++++++ .../com/blankj/utilcode/utils/PhoneUtils.java | 2 +- .../blankj/utilcode/utils/PinyinUtils.java | 211 ++++++++++++++++++ .../blankj/utilcode/utils/StringUtils.java | 193 +--------------- .../utilcode/utils/PinyinUtilsTest.java | 24 ++ .../utilcode/utils/StringUtilsTest.java | 19 +- .../com/blankj/utilcode/utils/TestUtils.java | 5 +- 13 files changed, 544 insertions(+), 223 deletions(-) create mode 100644 app/src/main/java/com/blankj/androidutilcode/activities/LocationActivity.java create mode 100644 app/src/main/res/layout/activity_location.xml create mode 100644 utilcode/src/main/java/com/blankj/utilcode/utils/LocationUtils.java create mode 100644 utilcode/src/main/java/com/blankj/utilcode/utils/PinyinUtils.java create mode 100644 utilcode/src/test/java/com/blankj/utilcode/utils/PinyinUtilsTest.java diff --git a/README-CN.md b/README-CN.md index 814af866ad..8f0f578da8 100644 --- a/README-CN.md +++ b/README-CN.md @@ -1,12 +1,12 @@ ## Android开发人员不得不收集的代码([持续更新中][update_log.md]) -**[README of English][readme.md]→[一键生成英文版README函数][trans]** +**[README of English][readme.md]** *** 为方便查找,已进行大致归类,其目录如下所示: > - **Activity相关→[ActivityUtils.java][activity.java]** ``` isActivityExists : 判断是否存在Activity launchActivity : 打开Activity -getLauncherActivity : 获取launcheractivity +getLauncherActivity : 获取入口activity ``` > - **App相关→[AppUtils.java][app.java]** @@ -283,10 +283,10 @@ getDomainAddress : 获取域名ip地址 > - **手机相关→[PhoneUtils.java][phone.java]** ``` isPhone : 判断设备是否是手机 -getIMEI : 获取IMIE码 +getIMEI : 获取IMEI码 getIMSI : 获取IMSI码 getPhoneType : 获取移动终端类型 -isSimCardReady : 判断是否存在sim卡 +isSimCardReady : 判断sim卡是否准备好 getSimOperatorName, getSimOperatorByMnc : 获取Sim卡运营商名称 getPhoneStatus : 获取手机状态信息 dial : 跳至拨号界面 @@ -298,26 +298,36 @@ getContactNum : 打开手机联系人界面点击联 getAllSMS : 获取手机短信并保存到xml中 ``` +> - **拼音相关→[PinyinUtils.java][pinyin.java]→[Test][pinyin.test]** + ``` +getPinyinFirstLetter : 获取第一个汉字首字母 +ccs2Pinyin : 汉字转拼音 + ``` + > - **进程相关→[ProcessUtils.java][process.java]** ``` -getForegroundProcessName : 获取前台线程包名 -cleanAllBackgroundProcesses,cleanBackgroundProcesses : 清理后台服务进程 +getForegroundProcessName : 获取前台线程包名 +cleanAllBackgroundProcesses, cleanBackgroundProcesses : 清理后台服务进程 ``` > - **正则相关→[RegexUtils.java][regex.java]→[Test][regex.test]** ``` -isMobileSimple : 验证手机号(简单) -isMobileExact : 验证手机号(精确) -isTel : 验证电话号码 -isIDCard15 : 验证身份证号码15位 -isIDCard18 : 验证身份证号码18位 -isEmail : 验证邮箱 -isURL : 验证URL -isChz : 验证汉字 -isUsername : 验证用户名 -isDate : 验证yyyy-MM-dd格式的日期校验,已考虑平闰年 -isIP : 验证IP地址 -isMatch : string是否匹配regex +isMobileSimple : 验证手机号(简单) +isMobileExact : 验证手机号(精确) +isTel : 验证电话号码 +isIDCard15 : 验证身份证号码15位 +isIDCard18 : 验证身份证号码18位 +isEmail : 验证邮箱 +isURL : 验证URL +isZh : 验证汉字 +isUsername : 验证用户名 +isDate : 验证yyyy-MM-dd格式的日期校验,已考虑平闰年 +isIP : 验证IP地址 +isMatch : 判断是否匹配正则 +getMatches : 获取正则匹配的部分 +getSplits : 获取正则匹配分组 +getReplaceFirst : 替换正则匹配的第一部分 +getReplaceAll : 替换所有正则匹配的部分 ``` > - **屏幕相关→[ScreenUtils.java][screen.java]** @@ -397,6 +407,7 @@ clear : SP中清除所有数据 isEmpty : 判断字符串是否为null或长度为0 isSpace : 判断字符串是否为null或全为空格 equals : 判断两字符串是否相等 +equalsIgnoreCase : 判断两字符串忽略大小写是否相等 null2Length0 : null转为长度为0的字符串 length : 返回字符串长度 upperFirstLetter : 首字母大写 @@ -404,8 +415,6 @@ lowerFirstLetter : 首字母小写 reverse : 反转字符串 toDBC : 转化为半角字符 toSBC : 转化为全角字符 -getPYFirstLetter : 获得第一个汉字首字母 -cn2PY : 中文转拼音 ``` > - **线程池相关→[ThreadPoolUtils.java][thread_pool.java]** @@ -510,8 +519,6 @@ limitations under the License. [readme.md]: https://github.com/Blankj/AndroidUtilCode [readme-cn.md]: https://github.com/Blankj/AndroidUtilCode/blob/master/README-CN.md -[trans]: https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/test/java/com/blankj/utilcode/utils/TestUtils.java - [activity.java]: https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/main/java/com/blankj/utilcode/utils/ActivityUtils.java [app.java]: https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/main/java/com/blankj/utilcode/utils/AppUtils.java @@ -558,6 +565,9 @@ limitations under the License. [phone.java]: https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/main/java/com/blankj/utilcode/utils/PhoneUtils.java +[pinyin.java]: https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/main/java/com/blankj/utilcode/utils/PinyinUtils.java +[pinyin.test]: https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/test/java/com/blankj/utilcode/utils/PinyinUtilsTest.java + [process.java]: https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/main/java/com/blankj/utilcode/utils/ProcessUtils.java [regex.java]: https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/main/java/com/blankj/utilcode/utils/RegexUtils.java diff --git a/README.md b/README.md index 23c8af8090..781108e6b0 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ ## Android developers should collect the following utils -**[中文版README][readme-cn.md]→[How to get this README from README-CN][trans]** +**[中文版README][readme-cn.md]** *** Directory is shown below: > - **About Activity→[ActivityUtils.java][activity.java]** @@ -298,10 +298,16 @@ getContactNum getAllSMS ``` +> - **About Pinyin→[PinyinUtils.java][pinyin.java]→[Test][pinyin.test]** + ``` +getPinyinFirstLetter +ccs2Pinyin + ``` + > - **About Process→[ProcessUtils.java][process.java]** ``` getForegroundProcessName -cleanAllBackgroundProcesses,cleanBackgroundProcesses +cleanAllBackgroundProcesses, cleanBackgroundProcesses ``` > - **About Regex→[RegexUtils.java][regex.java]→[Test][regex.test]** @@ -313,11 +319,15 @@ isIDCard15 isIDCard18 isEmail isURL -isChz +isZh isUsername isDate isIP isMatch +getMatches +getSplits +getReplaceFirst +getReplaceAll ``` > - **About Screen→[ScreenUtils.java][screen.java]** @@ -397,6 +407,7 @@ clear isEmpty isSpace equals +equalsIgnoreCase null2Length0 length upperFirstLetter @@ -404,8 +415,6 @@ lowerFirstLetter reverse toDBC toSBC -getPYFirstLetter -cn2PY ``` > - **About ThreadPool→[ThreadPoolUtils.java][thread_pool.java]** @@ -510,8 +519,6 @@ limitations under the License. [readme.md]: https://github.com/Blankj/AndroidUtilCode [readme-cn.md]: https://github.com/Blankj/AndroidUtilCode/blob/master/README-CN.md -[trans]: https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/test/java/com/blankj/utilcode/utils/TestUtils.java - [activity.java]: https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/main/java/com/blankj/utilcode/utils/ActivityUtils.java [app.java]: https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/main/java/com/blankj/utilcode/utils/AppUtils.java @@ -558,6 +565,9 @@ limitations under the License. [phone.java]: https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/main/java/com/blankj/utilcode/utils/PhoneUtils.java +[pinyin.java]: https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/main/java/com/blankj/utilcode/utils/PinyinUtils.java +[pinyin.test]: https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/test/java/com/blankj/utilcode/utils/PinyinUtilsTest.java + [process.java]: https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/main/java/com/blankj/utilcode/utils/ProcessUtils.java [regex.java]: https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/main/java/com/blankj/utilcode/utils/RegexUtils.java diff --git a/app/src/main/java/com/blankj/androidutilcode/activities/LocationActivity.java b/app/src/main/java/com/blankj/androidutilcode/activities/LocationActivity.java new file mode 100644 index 0000000000..1cf4befa2d --- /dev/null +++ b/app/src/main/java/com/blankj/androidutilcode/activities/LocationActivity.java @@ -0,0 +1,32 @@ +package com.blankj.androidutilcode.activities; + +import android.app.Activity; +import android.os.Bundle; +import android.widget.TextView; + +import com.blankj.androidutilcode.R; +import com.blankj.utilcode.utils.LocationUtils; + +/** + *
+ * author: Blankj + * blog : http://blankj.com + * time : 2016/10/13 + * desc : Location工具类测试 + *+ */ + +public class LocationActivity extends Activity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_activity); + + TextView tvAboutLocation = (TextView) findViewById(R.id.tv_about_location); + + LocationUtils.getInstance(this).init(new); + + tvAboutLocation.setText(""); + } +} diff --git a/app/src/main/res/layout/activity_location.xml b/app/src/main/res/layout/activity_location.xml new file mode 100644 index 0000000000..87a78bc309 --- /dev/null +++ b/app/src/main/res/layout/activity_location.xml @@ -0,0 +1,23 @@ + +
+ * author: Blankj + * blog : http://blankj.com + * time : 16/11/13 + * desc : 定位相关工具类 + *+ */ +public class LocationUtils { + + private volatile static LocationUtils uniqueInstance; + + private OnLocationChangeListener mListener; + + private MyLocationListener myLocationListener; + + private LocationManager mLocationManager; + + private Context mContext; + + private LocationUtils(Context context) { + mContext = context; + mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); + myLocationListener = new MyLocationListener(); + } + + /** + * 获取单例 + * + * @param context 上下文 + * @return LocationUtils对象 + */ + public static LocationUtils getInstance(Context context) { + if (uniqueInstance == null) { + synchronized (LocationUtils.class) { + if (uniqueInstance == null) { + uniqueInstance = new LocationUtils(context); + } + } + } + return uniqueInstance; + } + + /** + * 判断Gps是否打开 + * + * @return {@code true}: 是
需添加权限 {@code
+ * author: Blankj + * blog : http://blankj.com + * time : 16/11/16 + * desc : 拼音相关工具类 + *+ */ +public class PinyinUtils { + + private PinyinUtils() { + throw new UnsupportedOperationException("u can't instantiate me..."); + } + + private static int[] pinyinValue = new int[]{-20319, -20317, -20304, -20295, -20292, -20283, -20265, -20257, -20242, + -20230, -20051, -20036, -20032, + -20026, -20002, -19990, -19986, -19982, -19976, -19805, -19784, -19775, -19774, -19763, -19756, -19751, + -19746, -19741, -19739, -19728, + -19725, -19715, -19540, -19531, -19525, -19515, -19500, -19484, -19479, -19467, -19289, -19288, -19281, + -19275, -19270, -19263, -19261, + -19249, -19243, -19242, -19238, -19235, -19227, -19224, -19218, -19212, -19038, -19023, -19018, -19006, + -19003, -18996, -18977, -18961, + -18952, -18783, -18774, -18773, -18763, -18756, -18741, -18735, -18731, -18722, -18710, -18697, -18696, + -18526, -18518, -18501, -18490, + -18478, -18463, -18448, -18447, -18446, -18239, -18237, -18231, -18220, -18211, -18201, -18184, -18183, + -18181, -18012, -17997, -17988, + -17970, -17964, -17961, -17950, -17947, -17931, -17928, -17922, -17759, -17752, -17733, -17730, -17721, + -17703, -17701, -17697, -17692, + -17683, -17676, -17496, -17487, -17482, -17468, -17454, -17433, -17427, -17417, -17202, -17185, -16983, + -16970, -16942, -16915, -16733, + -16708, -16706, -16689, -16664, -16657, -16647, -16474, -16470, -16465, -16459, -16452, -16448, -16433, + -16429, -16427, -16423, -16419, + -16412, -16407, -16403, -16401, -16393, -16220, -16216, -16212, -16205, -16202, -16187, -16180, -16171, + -16169, -16158, -16155, -15959, + -15958, -15944, -15933, -15920, -15915, -15903, -15889, -15878, -15707, -15701, -15681, -15667, -15661, + -15659, -15652, -15640, -15631, + -15625, -15454, -15448, -15436, -15435, -15419, -15416, -15408, -15394, -15385, -15377, -15375, -15369, + -15363, -15362, -15183, -15180, + -15165, -15158, -15153, -15150, -15149, -15144, -15143, -15141, -15140, -15139, -15128, -15121, -15119, + -15117, -15110, -15109, -14941, + -14937, -14933, -14930, -14929, -14928, -14926, -14922, -14921, -14914, -14908, -14902, -14894, -14889, + -14882, -14873, -14871, -14857, + -14678, -14674, -14670, -14668, -14663, -14654, -14645, -14630, -14594, -14429, -14407, -14399, -14384, + -14379, -14368, -14355, -14353, + -14345, -14170, -14159, -14151, -14149, -14145, -14140, -14137, -14135, -14125, -14123, -14122, -14112, + -14109, -14099, -14097, -14094, + -14092, -14090, -14087, -14083, -13917, -13914, -13910, -13907, -13906, -13905, -13896, -13894, -13878, + -13870, -13859, -13847, -13831, + -13658, -13611, -13601, -13406, -13404, -13400, -13398, -13395, -13391, -13387, -13383, -13367, -13359, + -13356, -13343, -13340, -13329, + -13326, -13318, -13147, -13138, -13120, -13107, -13096, -13095, -13091, -13076, -13068, -13063, -13060, + -12888, -12875, -12871, -12860, + -12858, -12852, -12849, -12838, -12831, -12829, -12812, -12802, -12607, -12597, -12594, -12585, -12556, + -12359, -12346, -12320, -12300, + -12120, -12099, -12089, -12074, -12067, -12058, -12039, -11867, -11861, -11847, -11831, -11798, -11781, + -11604, -11589, -11536, -11358, + -11340, -11339, -11324, -11303, -11097, -11077, -11067, -11055, -11052, -11045, -11041, -11038, -11024, + -11020, -11019, -11018, -11014, + -10838, -10832, -10815, -10800, -10790, -10780, -10764, -10587, -10544, -10533, -10519, -10331, -10329, + -10328, -10322, -10315, -10309, + -10307, -10296, -10281, -10274, -10270, -10262, -10260, -10256, -10254}; + + private static String[] pinyin = new String[]{"a", "ai", "an", "ang", "ao", "ba", "bai", "ban", "bang", "bao", + "bei", "ben", "beng", "bi", "bian", + "biao", "bie", "bin", "bing", "bo", "bu", "ca", "cai", "can", "cang", "cao", "ce", "ceng", "cha", "chai", + "chan", "chang", "chao", "che", + "chen", "cheng", "chi", "chong", "chou", "chu", "chuai", "chuan", "chuang", "chui", "chun", "chuo", "ci", + "cong", "cou", "cu", "cuan", + "cui", "cun", "cuo", "da", "dai", "dan", "dang", "dao", "de", "deng", "di", "dian", "diao", "die", + "ding", "diu", "dong", "dou", "du", + "duan", "dui", "dun", "duo", "e", "en", "er", "fa", "fan", "fang", "fei", "fen", "feng", "fo", "fou", + "fu", "ga", "gai", "gan", "gang", + "gao", "ge", "gei", "gen", "geng", "gong", "gou", "gu", "gua", "guai", "guan", "guang", "gui", "gun", + "guo", "ha", "hai", "han", "hang", + "hao", "he", "hei", "hen", "heng", "hong", "hou", "hu", "hua", "huai", "huan", "huang", "hui", "hun", + "huo", "ji", "jia", "jian", + "jiang", "jiao", "jie", "jin", "jing", "jiong", "jiu", "ju", "juan", "jue", "jun", "ka", "kai", "kan", + "kang", "kao", "ke", "ken", + "keng", "kong", "kou", "ku", "kua", "kuai", "kuan", "kuang", "kui", "kun", "kuo", "la", "lai", "lan", + "lang", "lao", "le", "lei", "leng", + "li", "lia", "lian", "liang", "liao", "lie", "lin", "ling", "liu", "long", "lou", "lu", "lv", "luan", + "lue", "lun", "luo", "ma", "mai", + "man", "mang", "mao", "me", "mei", "men", "meng", "mi", "mian", "miao", "mie", "min", "ming", "miu", + "mo", "mou", "mu", "na", "nai", + "nan", "nang", "nao", "ne", "nei", "nen", "neng", "ni", "nian", "niang", "niao", "nie", "nin", "ning", + "niu", "nong", "nu", "nv", "nuan", + "nue", "nuo", "o", "ou", "pa", "pai", "pan", "pang", "pao", "pei", "pen", "peng", "pi", "pian", "piao", + "pie", "pin", "ping", "po", "pu", + "qi", "qia", "qian", "qiang", "qiao", "qie", "qin", "qing", "qiong", "qiu", "qu", "quan", "que", "qun", + "ran", "rang", "rao", "re", + "ren", "reng", "ri", "rong", "rou", "ru", "ruan", "rui", "run", "ruo", "sa", "sai", "san", "sang", "sao", + "se", "sen", "seng", "sha", + "shai", "shan", "shang", "shao", "she", "shen", "sheng", "shi", "shou", "shu", "shua", "shuai", "shuan", + "shuang", "shui", "shun", + "shuo", "si", "song", "sou", "su", "suan", "sui", "sun", "suo", "ta", "tai", "tan", "tang", "tao", "te", + "teng", "ti", "tian", "tiao", + "tie", "ting", "tong", "tou", "tu", "tuan", "tui", "tun", "tuo", "wa", "wai", "wan", "wang", "wei", + "wen", "weng", "wo", "wu", "xi", + "xia", "xian", "xiang", "xiao", "xie", "xin", "xing", "xiong", "xiu", "xu", "xuan", "xue", "xun", "ya", + "yan", "yang", "yao", "ye", "yi", + "yin", "ying", "yo", "yong", "you", "yu", "yuan", "yue", "yun", "za", "zai", "zan", "zang", "zao", "ze", + "zei", "zen", "zeng", "zha", + "zhai", "zhan", "zhang", "zhao", "zhe", "zhen", "zheng", "zhi", "zhong", "zhou", "zhu", "zhua", "zhuai", + "zhuan", "zhuang", "zhui", + "zhun", "zhuo", "zi", "zong", "zou", "zu", "zuan", "zui", "zun", "zuo"}; + + /** + * 单个汉字转成ASCII码 + * + * @param cc 单个汉字(Chinese character) + * @return 如果字符串长度是1返回的是对应的ascii码,否则返回-1 + */ + private static int oneCc2ASCII(String cc) { + if (cc.length() != 1) return -1; + int ascii = 0; + try { + byte[] bytes = cc.getBytes("GB2312"); + if (bytes.length == 1) { + ascii = bytes[0]; + } else if (bytes.length == 2) { + int highByte = 256 + bytes[0]; + int lowByte = 256 + bytes[1]; + ascii = (256 * highByte + lowByte) - 256 * 256; + } else { + throw new IllegalArgumentException("Illegal resource string"); + } + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + return ascii; + } + + /** + * 单个汉字转成拼音 + * + * @param cc 单个汉字(Chinese character) + * @return 如果字符串长度是1返回的是对应的拼音,否则返回{@code null} + */ + private static String oneCc2Pinyin(String cc) { + int ascii = oneCc2ASCII(cc); + if (ascii == -1) return null; + String ret = null; + if (0 <= ascii && ascii <= 127) { + ret = String.valueOf((char) ascii); + } else { + for (int i = pinyinValue.length - 1; i >= 0; i--) { + if (pinyinValue[i] <= ascii) { + ret = pinyin[i]; + break; + } + } + } + return ret; + } + + /** + * 获取第一个汉字首字母 + * + * @param ccs 汉字字符串(Chinese characters) + * @return 拼音 + */ + public static String getPinyinFirstLetter(String ccs) { + if (ccs == null || ccs.trim().length() <= 0) return null; + String firstCc, py; + firstCc = ccs.substring(0, 1); + py = oneCc2Pinyin(firstCc); + if (py == null) return null; + return py.substring(0, 1); + } + + /** + * 汉字转拼音 + * + * @param ccs 汉字字符串(Chinese characters) + * @return 拼音 + */ + public static String ccs2Pinyin(String ccs) { + String cc, py; + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < ccs.length(); i++) { + cc = ccs.substring(i, i + 1); + py = oneCc2Pinyin(cc); + if (py == null) py = "?"; + sb.append(py); + } + return sb.toString(); + } + + /** + * 汉字转拼音 + * + * @param ccs 汉字字符串 + * @param split 汉字拼音之间的分隔符 + * @return 拼音 + */ + public static String ccs2Pinyin(String ccs, String split) { + String cc, py; + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < ccs.length(); i++) { + cc = ccs.substring(i, i + 1); + py = oneCc2Pinyin(cc); + if (py == null) py = "?"; + sb.append(py).append(split); + } + return sb.toString(); + } +} diff --git a/utilcode/src/main/java/com/blankj/utilcode/utils/StringUtils.java b/utilcode/src/main/java/com/blankj/utilcode/utils/StringUtils.java index 646cd124d8..121183ac5a 100644 --- a/utilcode/src/main/java/com/blankj/utilcode/utils/StringUtils.java +++ b/utilcode/src/main/java/com/blankj/utilcode/utils/StringUtils.java @@ -1,7 +1,5 @@ package com.blankj.utilcode.utils; -import java.io.UnsupportedEncodingException; - /** *
* author: Blankj @@ -59,6 +57,17 @@ public static boolean equals(CharSequence a, CharSequence b) { return false; } + /** + * 判断两字符串忽略大小写是否相等 + * + * @param a 待校验字符串a + * @param b 待校验字符串b + * @return {@code true}: 相等
{@code false}: 不相等 + */ + public static boolean equalsIgnoreCase(String a, String b) { + return (a == b) || (b != null) && (a.length() == b.length()) && a.regionMatches(true, 0, b, 0, b.length()); + } + /** * null转为长度为0的字符串 * @@ -86,9 +95,7 @@ public static int length(CharSequence s) { * @return 首字母大写字符串 */ public static String upperFirstLetter(String s) { - if (isEmpty(s) || !Character.isLowerCase(s.charAt(0))) { - return s; - } + if (isEmpty(s) || !Character.isLowerCase(s.charAt(0))) return s; return String.valueOf((char) (s.charAt(0) - 32)) + s.substring(1); } @@ -170,180 +177,4 @@ public static String toSBC(String s) { } return new String(chars); } - - private static int[] pyValue = new int[]{-20319, -20317, -20304, -20295, -20292, -20283, -20265, -20257, -20242, - -20230, -20051, -20036, -20032, - -20026, -20002, -19990, -19986, -19982, -19976, -19805, -19784, -19775, -19774, -19763, -19756, -19751, - -19746, -19741, -19739, -19728, - -19725, -19715, -19540, -19531, -19525, -19515, -19500, -19484, -19479, -19467, -19289, -19288, -19281, - -19275, -19270, -19263, -19261, - -19249, -19243, -19242, -19238, -19235, -19227, -19224, -19218, -19212, -19038, -19023, -19018, -19006, - -19003, -18996, -18977, -18961, - -18952, -18783, -18774, -18773, -18763, -18756, -18741, -18735, -18731, -18722, -18710, -18697, -18696, - -18526, -18518, -18501, -18490, - -18478, -18463, -18448, -18447, -18446, -18239, -18237, -18231, -18220, -18211, -18201, -18184, -18183, - -18181, -18012, -17997, -17988, - -17970, -17964, -17961, -17950, -17947, -17931, -17928, -17922, -17759, -17752, -17733, -17730, -17721, - -17703, -17701, -17697, -17692, - -17683, -17676, -17496, -17487, -17482, -17468, -17454, -17433, -17427, -17417, -17202, -17185, -16983, - -16970, -16942, -16915, -16733, - -16708, -16706, -16689, -16664, -16657, -16647, -16474, -16470, -16465, -16459, -16452, -16448, -16433, - -16429, -16427, -16423, -16419, - -16412, -16407, -16403, -16401, -16393, -16220, -16216, -16212, -16205, -16202, -16187, -16180, -16171, - -16169, -16158, -16155, -15959, - -15958, -15944, -15933, -15920, -15915, -15903, -15889, -15878, -15707, -15701, -15681, -15667, -15661, - -15659, -15652, -15640, -15631, - -15625, -15454, -15448, -15436, -15435, -15419, -15416, -15408, -15394, -15385, -15377, -15375, -15369, - -15363, -15362, -15183, -15180, - -15165, -15158, -15153, -15150, -15149, -15144, -15143, -15141, -15140, -15139, -15128, -15121, -15119, - -15117, -15110, -15109, -14941, - -14937, -14933, -14930, -14929, -14928, -14926, -14922, -14921, -14914, -14908, -14902, -14894, -14889, - -14882, -14873, -14871, -14857, - -14678, -14674, -14670, -14668, -14663, -14654, -14645, -14630, -14594, -14429, -14407, -14399, -14384, - -14379, -14368, -14355, -14353, - -14345, -14170, -14159, -14151, -14149, -14145, -14140, -14137, -14135, -14125, -14123, -14122, -14112, - -14109, -14099, -14097, -14094, - -14092, -14090, -14087, -14083, -13917, -13914, -13910, -13907, -13906, -13905, -13896, -13894, -13878, - -13870, -13859, -13847, -13831, - -13658, -13611, -13601, -13406, -13404, -13400, -13398, -13395, -13391, -13387, -13383, -13367, -13359, - -13356, -13343, -13340, -13329, - -13326, -13318, -13147, -13138, -13120, -13107, -13096, -13095, -13091, -13076, -13068, -13063, -13060, - -12888, -12875, -12871, -12860, - -12858, -12852, -12849, -12838, -12831, -12829, -12812, -12802, -12607, -12597, -12594, -12585, -12556, - -12359, -12346, -12320, -12300, - -12120, -12099, -12089, -12074, -12067, -12058, -12039, -11867, -11861, -11847, -11831, -11798, -11781, - -11604, -11589, -11536, -11358, - -11340, -11339, -11324, -11303, -11097, -11077, -11067, -11055, -11052, -11045, -11041, -11038, -11024, - -11020, -11019, -11018, -11014, - -10838, -10832, -10815, -10800, -10790, -10780, -10764, -10587, -10544, -10533, -10519, -10331, -10329, - -10328, -10322, -10315, -10309, - -10307, -10296, -10281, -10274, -10270, -10262, -10260, -10256, -10254}; - - private static String[] pyStr = new String[]{"a", "ai", "an", "ang", "ao", "ba", "bai", "ban", "bang", "bao", - "bei", "ben", "beng", "bi", "bian", - "biao", "bie", "bin", "bing", "bo", "bu", "ca", "cai", "can", "cang", "cao", "ce", "ceng", "cha", "chai", - "chan", "chang", "chao", "che", - "chen", "cheng", "chi", "chong", "chou", "chu", "chuai", "chuan", "chuang", "chui", "chun", "chuo", "ci", - "cong", "cou", "cu", "cuan", - "cui", "cun", "cuo", "da", "dai", "dan", "dang", "dao", "de", "deng", "di", "dian", "diao", "die", - "ding", "diu", "dong", "dou", "du", - "duan", "dui", "dun", "duo", "e", "en", "er", "fa", "fan", "fang", "fei", "fen", "feng", "fo", "fou", - "fu", "ga", "gai", "gan", "gang", - "gao", "ge", "gei", "gen", "geng", "gong", "gou", "gu", "gua", "guai", "guan", "guang", "gui", "gun", - "guo", "ha", "hai", "han", "hang", - "hao", "he", "hei", "hen", "heng", "hong", "hou", "hu", "hua", "huai", "huan", "huang", "hui", "hun", - "huo", "ji", "jia", "jian", - "jiang", "jiao", "jie", "jin", "jing", "jiong", "jiu", "ju", "juan", "jue", "jun", "ka", "kai", "kan", - "kang", "kao", "ke", "ken", - "keng", "kong", "kou", "ku", "kua", "kuai", "kuan", "kuang", "kui", "kun", "kuo", "la", "lai", "lan", - "lang", "lao", "le", "lei", "leng", - "li", "lia", "lian", "liang", "liao", "lie", "lin", "ling", "liu", "long", "lou", "lu", "lv", "luan", - "lue", "lun", "luo", "ma", "mai", - "man", "mang", "mao", "me", "mei", "men", "meng", "mi", "mian", "miao", "mie", "min", "ming", "miu", - "mo", "mou", "mu", "na", "nai", - "nan", "nang", "nao", "ne", "nei", "nen", "neng", "ni", "nian", "niang", "niao", "nie", "nin", "ning", - "niu", "nong", "nu", "nv", "nuan", - "nue", "nuo", "o", "ou", "pa", "pai", "pan", "pang", "pao", "pei", "pen", "peng", "pi", "pian", "piao", - "pie", "pin", "ping", "po", "pu", - "qi", "qia", "qian", "qiang", "qiao", "qie", "qin", "qing", "qiong", "qiu", "qu", "quan", "que", "qun", - "ran", "rang", "rao", "re", - "ren", "reng", "ri", "rong", "rou", "ru", "ruan", "rui", "run", "ruo", "sa", "sai", "san", "sang", "sao", - "se", "sen", "seng", "sha", - "shai", "shan", "shang", "shao", "she", "shen", "sheng", "shi", "shou", "shu", "shua", "shuai", "shuan", - "shuang", "shui", "shun", - "shuo", "si", "song", "sou", "su", "suan", "sui", "sun", "suo", "ta", "tai", "tan", "tang", "tao", "te", - "teng", "ti", "tian", "tiao", - "tie", "ting", "tong", "tou", "tu", "tuan", "tui", "tun", "tuo", "wa", "wai", "wan", "wang", "wei", - "wen", "weng", "wo", "wu", "xi", - "xia", "xian", "xiang", "xiao", "xie", "xin", "xing", "xiong", "xiu", "xu", "xuan", "xue", "xun", "ya", - "yan", "yang", "yao", "ye", "yi", - "yin", "ying", "yo", "yong", "you", "yu", "yuan", "yue", "yun", "za", "zai", "zan", "zang", "zao", "ze", - "zei", "zen", "zeng", "zha", - "zhai", "zhan", "zhang", "zhao", "zhe", "zhen", "zheng", "zhi", "zhong", "zhou", "zhu", "zhua", "zhuai", - "zhuan", "zhuang", "zhui", - "zhun", "zhuo", "zi", "zong", "zou", "zu", "zuan", "zui", "zun", "zuo"}; - - /** - * 单个汉字转成ASCII码 - * - * @param s 单个汉字字符串 - * @return 如果字符串长度是1返回的是对应的ascii码,否则返回-1 - */ - private static int oneCn2ASCII(String s) { - if (s.length() != 1) return -1; - int ascii = 0; - try { - byte[] bytes = s.getBytes("GB2312"); - if (bytes.length == 1) { - ascii = bytes[0]; - } else if (bytes.length == 2) { - int highByte = 256 + bytes[0]; - int lowByte = 256 + bytes[1]; - ascii = (256 * highByte + lowByte) - 256 * 256; - } else { - throw new IllegalArgumentException("Illegal resource string"); - } - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } - return ascii; - } - - /** - * 单个汉字转成拼音 - * - * @param s 单个汉字字符串 - * @return 如果字符串长度是1返回的是对应的拼音,否则返回{@code null} - */ - private static String oneCn2PY(String s) { - int ascii = oneCn2ASCII(s); - if (ascii == -1) return null; - String ret = null; - if (0 <= ascii && ascii <= 127) { - ret = String.valueOf((char) ascii); - } else { - for (int i = pyValue.length - 1; i >= 0; i--) { - if (pyValue[i] <= ascii) { - ret = pyStr[i]; - break; - } - } - } - return ret; - } - - /** - * 获得第一个汉字首字母 - * - * @param s 单个汉字字符串 - * @return 拼音 - */ - public static String getPYFirstLetter(String s) { - if (isSpace(s)) return ""; - String first, py; - first = s.substring(0, 1); - py = oneCn2PY(first); - if (py == null) return null; - return py.substring(0, 1); - } - - /** - * 中文转拼音 - * - * @param s 汉字字符串 - * @return 拼音 - */ - public static String cn2PY(String s) { - String hz, py; - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < s.length(); i++) { - hz = s.substring(i, i + 1); - py = oneCn2PY(hz); - if (py == null) { - py = "?"; - } - sb.append(py); - } - return sb.toString(); - } } \ No newline at end of file diff --git a/utilcode/src/test/java/com/blankj/utilcode/utils/PinyinUtilsTest.java b/utilcode/src/test/java/com/blankj/utilcode/utils/PinyinUtilsTest.java new file mode 100644 index 0000000000..a4c6210bf0 --- /dev/null +++ b/utilcode/src/test/java/com/blankj/utilcode/utils/PinyinUtilsTest.java @@ -0,0 +1,24 @@ +package com.blankj.utilcode.utils; + +import org.junit.Test; + +/** + *+ * author: Blankj + * blog : http://blankj.com + * time : 16/11/16 + * desc : + *+ */ +public class PinyinUtilsTest { + @Test + public void getPinyinFirstLetter() throws Exception { + System.out.println(PinyinUtils.getPinyinFirstLetter("杰")); + } + + @Test + public void cn2Pinyin() throws Exception { + System.out.println(PinyinUtils.ccs2Pinyin("测试中文转拼音")); + System.out.println(PinyinUtils.ccs2Pinyin("测试中文转拼音", " ")); + } +} \ No newline at end of file diff --git a/utilcode/src/test/java/com/blankj/utilcode/utils/StringUtilsTest.java b/utilcode/src/test/java/com/blankj/utilcode/utils/StringUtilsTest.java index c38b321ec7..15dba435b2 100644 --- a/utilcode/src/test/java/com/blankj/utilcode/utils/StringUtilsTest.java +++ b/utilcode/src/test/java/com/blankj/utilcode/utils/StringUtilsTest.java @@ -30,6 +30,19 @@ public void testIsSpace() throws Exception { assertThat(isSpace(" ")).isFalse(); } + @Test + public void testEquals() throws Exception { + assertThat(StringUtils.equals("blankj", "blankj")).isEqualTo(true); + assertThat(StringUtils.equals("blankj", "Blankj")).isEqualTo(false); + } + + @Test + public void testEqualsIgnoreCase() throws Exception { + assertThat(equalsIgnoreCase("blankj", "Blankj")).isEqualTo(true); + assertThat(equalsIgnoreCase("blankj", "blankj")).isEqualTo(true); + assertThat(equalsIgnoreCase("blankj", "blank")).isEqualTo(false); + } + @Test public void testNull2Length0() throws Exception { assertThat(null2Length0(null)).isEqualTo(""); @@ -73,10 +86,4 @@ public void testToDBC() throws Exception { public void testToSBC() throws Exception { assertThat(toSBC(" ,.&")).isEqualTo(" ,.&"); } - - @Test - public void test() throws Exception { - System.out.println(StringUtils.getPYFirstLetter("c")); - System.out.println(StringUtils.cn2PY("测试转拼音")); - } } \ No newline at end of file diff --git a/utilcode/src/test/java/com/blankj/utilcode/utils/TestUtils.java b/utilcode/src/test/java/com/blankj/utilcode/utils/TestUtils.java index 31f740c95d..72d6c3b08c 100644 --- a/utilcode/src/test/java/com/blankj/utilcode/utils/TestUtils.java +++ b/utilcode/src/test/java/com/blankj/utilcode/utils/TestUtils.java @@ -10,6 +10,8 @@ import org.robolectric.annotation.Config; import java.io.File; +import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; /** @@ -40,7 +42,7 @@ public void readme2Eng() throws Exception { File readmeEng = new File(new File(System.getProperty("user.dir")).getParent() + SEP + "README.md"); Listlist = FileUtils.readFile2List(readmeCN, "UTF-8"); StringBuilder sb = new StringBuilder("## Android developers should collect the following utils\r\n" + - "**[中文版README][readme-cn.md]→[How to get this README from README-CN][trans]**\r\n" + + "**[中文版README][readme-cn.md]**\r\n" + "***\r\n" + "Directory is shown below: \r\n"); List lines = list.subList(4, list.size()); @@ -105,6 +107,5 @@ public void formatCN() throws Exception { @Test public void test() throws Exception { - } }