StringUtils.java 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  1. package com.wdkl.callingmainnurse.util;
  2. import android.content.Context;
  3. import android.content.pm.ApplicationInfo;
  4. import android.content.pm.PackageInfo;
  5. import android.content.pm.PackageManager;
  6. import android.content.pm.PackageManager.NameNotFoundException;
  7. import android.os.Bundle;
  8. import com.wdkl.callingmainnurse.BuildConfig;
  9. import com.wdkl.callingmainnurse.MyApplication;
  10. import com.wdkl.callingmainnurse.R;
  11. import com.wdkl.callingmainnurse.common.Constants;
  12. import java.io.BufferedReader;
  13. import java.io.IOException;
  14. import java.io.InputStream;
  15. import java.io.InputStreamReader;
  16. import java.io.UnsupportedEncodingException;
  17. import java.text.DecimalFormat;
  18. import java.text.ParseException;
  19. import java.text.SimpleDateFormat;
  20. import java.util.ArrayList;
  21. import java.util.Calendar;
  22. import java.util.Collection;
  23. import java.util.Date;
  24. import java.util.Iterator;
  25. import java.util.List;
  26. import java.util.regex.Matcher;
  27. import java.util.regex.Pattern;
  28. import serialporttest.utils.SerialPortUtil;
  29. /**
  30. * 类描述:字符串操作工具包
  31. * 创建人:Waderson Shll (TEL:15675117662)<br>
  32. * 创建时间:2017-11-2 <br>
  33. * 特别提醒:如有需要该类可任意调用;在未通知本人的情况下该类禁止任何修改!<br>
  34. *
  35. * @version 1.0
  36. */
  37. public class StringUtils {
  38. private final static Pattern emailer = Pattern
  39. .compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
  40. private final static Pattern backCard = Pattern
  41. .compile(" /^(\\[0-9]{16}|\\[0-9]{18}|\\[0-9]{19})$/;");
  42. // private final static SimpleDateFormat dateFormater = new
  43. // SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  44. // private final static SimpleDateFormat dateFormater2 = new
  45. // SimpleDateFormat("yyyy-MM-dd");
  46. private final static ThreadLocal<SimpleDateFormat> dateFormater = new ThreadLocal<SimpleDateFormat>() {
  47. @Override
  48. protected SimpleDateFormat initialValue() {
  49. return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  50. }
  51. };
  52. private final static ThreadLocal<SimpleDateFormat> dateFormater2 = new ThreadLocal<SimpleDateFormat>() {
  53. @Override
  54. protected SimpleDateFormat initialValue() {
  55. return new SimpleDateFormat("yyyy-MM-dd");
  56. }
  57. };
  58. public static String getAppVersionName(Context context) {
  59. try {
  60. PackageInfo info = context.getPackageManager().getPackageInfo(
  61. context.getPackageName(), 0);
  62. return info.versionName;
  63. // return info.versionCode;
  64. } catch (NameNotFoundException e) {
  65. e.printStackTrace();
  66. }
  67. return 1.0 + "";
  68. }
  69. public static String getAllVersionName() {
  70. String str = "\r\nApp名称: " + MyApplication.getAppContext().getString(R.string.app_name)
  71. + "\r\nAPP版本号: V" + BuildConfig.VERSION_NAME
  72. + "\r\nMCU版本号: " + Constants.MCU_VERSION_NUMBER
  73. + "\r\n设备ID: " + SerialPortUtil.KEY_ID
  74. + "\r\n中央服务器系列"
  75. + "\r\n发布日期:" + BuildConfig.BUILD_TIME;
  76. if (android.os.Build.VERSION.SDK_INT >= 26) { //android8.0 = 26
  77. str = str + " android8.1 , rk3368";// rk3368 android8.1
  78. } else if (android.os.Build.VERSION.SDK_INT >= 24) { //android7.0 = 24
  79. str = str + " android7.1 , rk3128";//rk3128 android7.1
  80. } else {
  81. str = str + " android4.2 , a20";// a20 android4.2
  82. }
  83. return str;
  84. }
  85. /**
  86. * 将字符串转为日期类型
  87. *
  88. * @param sdate
  89. * @return
  90. */
  91. public static Date toDate(String sdate) {
  92. try {
  93. return dateFormater.get().parse(sdate);
  94. } catch (ParseException e) {
  95. return null;
  96. }
  97. }
  98. /**
  99. * 以友好的方式显示时间
  100. *
  101. * @param sdate
  102. * @return
  103. */
  104. public static String friendly_time(String sdate) {
  105. Date time = toDate(sdate);
  106. if (time == null) {
  107. return "Unknown";
  108. }
  109. String ftime = "";
  110. Calendar cal = Calendar.getInstance();
  111. // 判断是否是同一天
  112. String curDate = dateFormater2.get().format(cal.getTime());
  113. String paramDate = dateFormater2.get().format(time);
  114. if (curDate.equals(paramDate)) {
  115. int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
  116. if (hour == 0)
  117. ftime = Math.max(
  118. (cal.getTimeInMillis() - time.getTime()) / 60000, 1)
  119. + "分钟前";
  120. else
  121. ftime = hour + "小时前";
  122. return ftime;
  123. }
  124. long lt = time.getTime() / 86400000;
  125. long ct = cal.getTimeInMillis() / 86400000;
  126. int days = (int) (ct - lt);
  127. if (days == 0) {
  128. int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
  129. if (hour == 0)
  130. ftime = Math.max(
  131. (cal.getTimeInMillis() - time.getTime()) / 60000, 1)
  132. + "分钟前";
  133. else
  134. ftime = hour + "小时前";
  135. } else if (days == 1) {
  136. ftime = "昨天";
  137. } else if (days == 2) {
  138. ftime = "前天";
  139. } else if (days > 2 && days <= 10) {
  140. ftime = days + "天前";
  141. } else if (days > 10) {
  142. ftime = dateFormater2.get().format(time);
  143. }
  144. return ftime;
  145. }
  146. /**
  147. * 判断给定字符串时间是否为今日
  148. *
  149. * @param sdate
  150. * @return boolean
  151. */
  152. public static boolean isToday(String sdate) {
  153. boolean b = false;
  154. Date time = toDate(sdate);
  155. Date today = new Date();
  156. if (time != null) {
  157. String nowDate = dateFormater2.get().format(today);
  158. String timeDate = dateFormater2.get().format(time);
  159. if (nowDate.equals(timeDate)) {
  160. b = true;
  161. }
  162. }
  163. return b;
  164. }
  165. /**
  166. * 判断给定字符串是否空白串。 空白串是指由空格、制表符、回车符、换行符组成的字符串 若输入字符串为null或空字符串,返回true
  167. *
  168. * @param input
  169. * @return boolean
  170. */
  171. public static boolean isEmpty(String input) {
  172. if (input == null || "".equals(input))
  173. return true;
  174. for (int i = 0; i < input.length(); i++) {
  175. char c = input.charAt(i);
  176. if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
  177. return false;
  178. }
  179. }
  180. return true;
  181. }
  182. public static boolean notEmpty(String input) {
  183. if (input != null && input.length() > 0
  184. && !input.equals("null") && !input.equals("NULL")) {
  185. return true;
  186. }
  187. return false;
  188. }
  189. public static boolean listNotEmpty(List list) {
  190. if (list != null && list.size() > 0) {
  191. return true;
  192. }
  193. return false;
  194. }
  195. /**
  196. * 判断是不是一个合法的银行卡
  197. *
  198. * @param cardNum
  199. * @return
  200. */
  201. public static boolean isCardNum(String cardNum) {
  202. if (cardNum == null || cardNum.trim().length() == 0)
  203. return false;
  204. return backCard.matcher(cardNum).matches();
  205. }
  206. /**
  207. * 判断是不是一个合法的电子邮件地址
  208. *
  209. * @param email
  210. * @return
  211. */
  212. public static boolean isEmail(String email) {
  213. if (email == null || email.trim().length() == 0)
  214. return false;
  215. return emailer.matcher(email).matches();
  216. }
  217. /**
  218. * 字符串转整数
  219. *
  220. * @param str
  221. * @param defValue
  222. * @return
  223. */
  224. public static int toInt(String str, int defValue) {
  225. try {
  226. return Integer.parseInt(str);
  227. } catch (Exception e) {
  228. }
  229. return defValue;
  230. }
  231. /**
  232. * 检查是否是金额(12.00)
  233. *
  234. * @param phone
  235. * @return
  236. */
  237. public static boolean checkNumEx(String phone) {
  238. Pattern pattern = Pattern.compile("^([0-9]+|[0-9]{1,3}(,[0-9]{3})*)(.[0-9]{1,2})?$");
  239. Matcher match = pattern.matcher(phone);
  240. return match.matches();
  241. }
  242. /**
  243. * 对象转整数
  244. *
  245. * @param obj
  246. * @return 转换异常返回 0
  247. */
  248. public static int toInt(Object obj) {
  249. if (obj == null)
  250. return 0;
  251. return toInt(obj.toString(), 0);
  252. }
  253. /**
  254. * 对象转整数
  255. *
  256. * @param obj
  257. * @return 转换异常返回 0
  258. */
  259. public static long toLong(String obj) {
  260. try {
  261. return Long.parseLong(obj);
  262. } catch (Exception e) {
  263. }
  264. return 0;
  265. }
  266. /**
  267. * 字符串转布尔值
  268. *
  269. * @param b
  270. * @return 转换异常返回 false
  271. */
  272. public static boolean toBool(String b) {
  273. try {
  274. return Boolean.parseBoolean(b);
  275. } catch (Exception e) {
  276. }
  277. return false;
  278. }
  279. /**
  280. * 是否是英文字
  281. *
  282. * @param c
  283. * @return
  284. */
  285. public static boolean isLetter(char c) {
  286. int k = 0x80;
  287. return c / k == 0 ? true : false;
  288. }
  289. /**
  290. * 是否是英文字
  291. *
  292. * @return
  293. */
  294. public static boolean isEnglish(String str) {
  295. if (!notEmpty(str)) return false;
  296. return str.matches("[a-zA-Z]+");
  297. }
  298. /**
  299. * 判断一个字符串是否包含某个字符
  300. *
  301. * @return
  302. */
  303. public static boolean isHaveStr(String old, String s) {
  304. if (!notEmpty(old) || !notEmpty(s)) return false;
  305. return old.contains(s);
  306. }
  307. /**
  308. * 获取英文字长度
  309. *
  310. * @param s
  311. * @return
  312. */
  313. public static int length(String s) {
  314. if (s == null)
  315. return 0;
  316. char[] c = s.toCharArray();
  317. int len = 0;
  318. int length = c.length;
  319. for (int i = 0; i < length; i++) {
  320. len++;
  321. if (!isLetter(c[i])) {
  322. len++;
  323. }
  324. }
  325. return len;
  326. }
  327. /**
  328. * 去掉html标签
  329. *
  330. * @param htmlStr
  331. * @return
  332. */
  333. public static String delHTMLTag(String htmlStr) {
  334. String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
  335. String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
  336. String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
  337. Pattern p_script = Pattern.compile(regEx_script,
  338. Pattern.CASE_INSENSITIVE);
  339. Matcher m_script = p_script.matcher(htmlStr);
  340. htmlStr = m_script.replaceAll(""); // 过滤script标签
  341. Pattern p_style = Pattern
  342. .compile(regEx_style, Pattern.CASE_INSENSITIVE);
  343. Matcher m_style = p_style.matcher(htmlStr);
  344. htmlStr = m_style.replaceAll(""); // 过滤style标签
  345. Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
  346. Matcher m_html = p_html.matcher(htmlStr);
  347. htmlStr = m_html.replaceAll(""); // 过滤html标签
  348. htmlStr = htmlStr.replaceAll("&nbsp;", "");
  349. return htmlStr.trim(); // 返回文本字符串
  350. }
  351. /**
  352. * 去掉换行符
  353. *
  354. * @param str
  355. * @return
  356. */
  357. public static String removeLineChar(String str) {
  358. if (str == null)
  359. return "";
  360. return str.replaceAll("\r\n", "").replaceAll("\n", "");
  361. }
  362. /**
  363. * 以中文字长度计算,截取字符串
  364. *
  365. * @param origin
  366. * @param len
  367. * @param c
  368. * @return
  369. */
  370. public static String substring(String origin, int len, String c) {
  371. if (origin == null || origin.equals("") || len < 1)
  372. return "";
  373. String temp = removeLineChar(origin);
  374. byte[] strByte = new byte[len];
  375. if (len > length(origin)) {
  376. return temp;
  377. }
  378. try {
  379. System.arraycopy(temp.getBytes("GBK"), 0, strByte, 0, len);
  380. int count = 0;
  381. for (int i = 0; i < len; i++) {
  382. int value = (int) strByte[i];
  383. if (value < 0) {
  384. count++;
  385. }
  386. }
  387. if (count % 2 != 0) {
  388. len = (len == 1) ? ++len : --len;
  389. }
  390. return new String(strByte, 0, len, "GBK") + c;
  391. } catch (UnsupportedEncodingException e) {
  392. throw new RuntimeException(e);
  393. }
  394. }
  395. public static int StrToIntDef(String s, int defaultValue) {
  396. int res = defaultValue;
  397. try {
  398. res = Integer.parseInt(s, 10);
  399. } catch (Exception e) {
  400. res = defaultValue;
  401. }
  402. return res;
  403. }
  404. /**
  405. * 将数据集合转化拼成字符串
  406. *
  407. * @param collection 集合
  408. * @param delimiter 分隔符
  409. * @return
  410. */
  411. public static String join(Collection<?> collection, String delimiter) {
  412. StringBuilder builder = new StringBuilder();
  413. Iterator<?> iter = collection.iterator();
  414. while (iter.hasNext()) {
  415. builder.append(iter.next());
  416. if (iter.hasNext()) {
  417. builder.append(delimiter);
  418. }
  419. }
  420. return builder.toString();
  421. }
  422. /**
  423. * 验证身份证是否有效15位或18位<包括对年月日的合法性进行验证>
  424. *
  425. * @param idcard
  426. * @return
  427. */
  428. public static boolean isIDCard(String idcard) {
  429. Pattern pattern = Pattern.compile("^\\d{15}(\\d{2}[0-9xX])?$");
  430. Matcher macher = pattern.matcher(idcard);
  431. if (macher.find()) {// 对年月日字符串的验证
  432. String power = idcard.substring(idcard.length() - 12,
  433. idcard.length() - 4);
  434. pattern = Pattern
  435. .compile("^[1-2]+([0-9]{3})+(0[1-9][0-2][0-9]|0[1-9]3[0-1]|1[0-2][0-3][0-1]|1[0-2][0-2][0-9])");
  436. macher = pattern.matcher(power);
  437. }
  438. return macher.find();
  439. }
  440. /**
  441. * 检查是否是正确的email
  442. *
  443. * @param email
  444. * @return
  445. */
  446. public static boolean checkEmail(String email) {
  447. String regex = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
  448. Pattern p = Pattern.compile(regex);
  449. Matcher m = p.matcher(email);
  450. return m.matches();
  451. }
  452. /**
  453. * 验证邮箱格式
  454. *
  455. * @param strEmail
  456. * @return
  457. */
  458. public static boolean isEmailOther(String strEmail) {
  459. String strPattern = "^[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";
  460. Pattern p = Pattern.compile(strPattern);
  461. Matcher m = p.matcher(strEmail);
  462. return m.matches();
  463. }
  464. /**
  465. * 判断姓名只能输入中文,或者英文
  466. *
  467. * @param name
  468. * @return
  469. */
  470. public static boolean isUserName(String name) {
  471. if (name.matches("(([\u4E00-\u9FA5]{2,7})|([a-zA-Z]{3,10}))")) {
  472. return true;
  473. }
  474. return false;
  475. }
  476. /**
  477. * 对银行卡进行加*号保护设置并格式化
  478. *
  479. * @param cardNo
  480. * @return 如:6222 **** **** 1234
  481. */
  482. public static String formateBankNumber(String cardNo) {
  483. String star = " **** **** ";
  484. int cardNoLength = cardNo.length();
  485. if (cardNoLength >= 4) {
  486. String start = cardNo.substring(0, 4);
  487. String end = cardNo.substring(cardNoLength - 4, cardNoLength);
  488. return start + star + end;
  489. } else {
  490. return cardNo;
  491. }
  492. }
  493. /**
  494. * 手机号码加星保护
  495. *
  496. * @param phone
  497. * @return 如:136*****256
  498. */
  499. public static String formatPhone(String phone) {
  500. String star = "******";
  501. return phone.substring(0, 3) + star
  502. + phone.substring(phone.length() - 3, phone.length());
  503. }
  504. /**
  505. * 检查是否是数字
  506. *
  507. * @param phone
  508. * @return
  509. */
  510. public static boolean checkNum(String phone) {
  511. Pattern pattern = Pattern.compile("[0-9]+");
  512. Matcher match = pattern.matcher(phone);
  513. return match.matches();
  514. }
  515. /**
  516. * 检查是否是电话号码
  517. */
  518. public static boolean isMobileNo(String paramString) {
  519. return Pattern.compile("^1[3|4|5|7|8]\\d{9}$").matcher(paramString)
  520. .matches();
  521. }
  522. /**
  523. * 检查用户输入账号格式是否正确,只能是邮箱或电话号码
  524. *
  525. * @param account
  526. * @return
  527. */
  528. public static boolean checkAccount(String account) {
  529. return (isMobileNo(account) || checkEmail(account));
  530. }
  531. // 校验Tag Alias 只能是数字,英文字母和中文
  532. public static boolean isValidTagAndAlias(String s) {
  533. Pattern p = Pattern.compile("^[\u4E00-\u9FA50-9a-zA-Z_-]{0,}$");
  534. Matcher m = p.matcher(s);
  535. return m.matches();
  536. }
  537. public static final String KEY_APP_KEY = "JPUSH_APPKEY";
  538. /**
  539. * 获取jpush AppKey
  540. *
  541. * @param context
  542. * @return
  543. */
  544. public static String getAppKey(Context context) {
  545. Bundle metaData = null;
  546. String appKey = null;
  547. try {
  548. ApplicationInfo ai = context.getPackageManager()
  549. .getApplicationInfo(context.getPackageName(),
  550. PackageManager.GET_META_DATA);
  551. if (null != ai)
  552. metaData = ai.metaData;
  553. if (null != metaData) {
  554. appKey = metaData.getString(KEY_APP_KEY);
  555. if ((null == appKey) || appKey.length() != 24) {
  556. appKey = null;
  557. }
  558. }
  559. } catch (NameNotFoundException e) {
  560. }
  561. return appKey;
  562. }
  563. /**
  564. * 将输入流转化为String
  565. *
  566. * @param is
  567. * @return
  568. */
  569. public static String readDataFromStream(InputStream is) {
  570. BufferedReader br = new BufferedReader(new InputStreamReader(is));
  571. StringBuilder builder = new StringBuilder();
  572. String line = null;
  573. try {
  574. while ((line = br.readLine()) != null) {
  575. builder.append(line);
  576. }
  577. } catch (Exception e) {
  578. e.printStackTrace();
  579. } finally {
  580. try {
  581. is.close();
  582. } catch (IOException e) {
  583. e.printStackTrace();
  584. }
  585. }
  586. return builder.toString();
  587. }
  588. /**
  589. * 补零操作
  590. */
  591. public static String zerofill(Object obj) {
  592. String parten = "00";
  593. if (obj instanceof String)
  594. obj = Double.parseDouble(obj.toString());
  595. DecimalFormat decimal = new DecimalFormat(parten);
  596. return decimal.format(obj);
  597. }
  598. /**
  599. * ASCII表中可见字符从!开始,偏移位值为33(Decimal)
  600. */
  601. static final char DBC_CHAR_START = 33; // 半角!
  602. /**
  603. * ASCII表中可见字符到~结束,偏移位值为126(Decimal)
  604. */
  605. static final char DBC_CHAR_END = 126; // 半角~
  606. /**
  607. * 全角对应于ASCII表的可见字符从!开始,偏移值为65281
  608. */
  609. static final char SBC_CHAR_START = 65281; // 全角!
  610. /**
  611. * 全角对应于ASCII表的可见字符到~结束,偏移值为65374
  612. */
  613. static final char SBC_CHAR_END = 65374; // 全角~
  614. /**
  615. * ASCII表中除空格外的可见字符与对应的全角字符的相对偏移
  616. */
  617. static final int CONVERT_STEP = 65248; // 全角半角转换间隔
  618. /**
  619. * 全角空格的值,它没有遵从与ASCII的相对偏移,必须单独处理
  620. */
  621. static final char SBC_SPACE = 12288; // 全角空格 12288
  622. /**
  623. * 半角空格的值,在ASCII中为32(Decimal)
  624. */
  625. static final char DBC_SPACE = ' '; // 半角空格
  626. /**
  627. * <PRE>
  628. * 半角字符->全角字符转换
  629. * 只处理空格,!到&tilde;之间的字符,忽略其他
  630. * </PRE>
  631. */
  632. public static String bj2qj(String src) {
  633. if (src == null) {
  634. return src;
  635. }
  636. StringBuilder buf = new StringBuilder(src.length());
  637. char[] ca = src.toCharArray();
  638. for (int i = 0; i < ca.length; i++) {
  639. if (ca[i] == DBC_SPACE) { // 如果是半角空格,直接用全角空格替代
  640. buf.append(SBC_SPACE);
  641. } else if ((ca[i] >= DBC_CHAR_START) && (ca[i] <= DBC_CHAR_END)) { // 字符是!到~之间的可见字符
  642. buf.append((char) (ca[i] + CONVERT_STEP));
  643. } else { // 不对空格以及ascii表中其他可见字符之外的字符做任何处理
  644. buf.append(ca[i]);
  645. }
  646. }
  647. return buf.toString();
  648. }
  649. /**
  650. * <PRE>
  651. * 全角字符->半角字符转换
  652. * 只处理全角的空格,全角!到全角~之间的字符,忽略其他
  653. * </PRE>
  654. */
  655. public static String qj2bj(String src) {
  656. if (src == null) {
  657. return src;
  658. }
  659. StringBuilder buf = new StringBuilder(src.length());
  660. char[] ca = src.toCharArray();
  661. for (int i = 0; i < src.length(); i++) {
  662. if (ca[i] >= SBC_CHAR_START && ca[i] <= SBC_CHAR_END) { // 如果位于全角!到全角~区间内
  663. buf.append((char) (ca[i] - CONVERT_STEP));
  664. } else if (ca[i] == SBC_SPACE) { // 如果是全角空格
  665. buf.append(DBC_SPACE);
  666. } else { // 不处理全角空格,全角!到全角~区间外的字符
  667. buf.append(ca[i]);
  668. }
  669. }
  670. return buf.toString();
  671. }
  672. /**
  673. * 字符串按索引截取
  674. *
  675. * @param str
  676. * @return
  677. */
  678. public static String substringByLengh(String str, int start, int end) {
  679. if (str == null) {
  680. return "";
  681. }
  682. if (start > end) {
  683. return "";
  684. }
  685. if (str.length() - 1 < start || str.length() < end) {
  686. return "";
  687. }
  688. return str.substring(start, end);
  689. }
  690. /**
  691. * 字符串按单个截取
  692. *
  693. * @param str
  694. * @return
  695. */
  696. public static ArrayList<String> substringBySing(String str) {
  697. if (null == str||"".equals(str)) {
  698. return null;
  699. }
  700. ArrayList<String> list=new ArrayList<>();
  701. char[] c=str.toCharArray();
  702. for(int i=0;i<c.length;i++) {
  703. String s = String.valueOf(c[i]);
  704. list.add(s);
  705. }
  706. return list;
  707. }
  708. /**
  709. * 追加内容到当前String对象<br>
  710. *
  711. * @return
  712. */
  713. public static String append(String oldStr, String appendStr) {
  714. if (oldStr == null) {
  715. return appendStr;
  716. }
  717. StringBuffer sb = new StringBuffer(oldStr);
  718. return sb.append(appendStr).toString();
  719. }
  720. /**
  721. * 删除指定位置的字符<br>
  722. *
  723. * @return
  724. */
  725. public static String deleteCharAt(String oldStr, int index) {
  726. if (oldStr == null) {
  727. return null;
  728. }
  729. if (oldStr.length() <= 0) {
  730. return "";
  731. }
  732. StringBuffer sb = new StringBuffer(oldStr);
  733. return sb.deleteCharAt(index).toString();
  734. }
  735. /**
  736. * String对象中插入内容<br>
  737. *
  738. * @return
  739. */
  740. public static String insert(String oldStr, String insertStr, int index) {
  741. if (oldStr == null) {
  742. return null;
  743. }
  744. StringBuffer sb = new StringBuffer(oldStr);
  745. return sb.insert(index, insertStr).toString();
  746. }
  747. /**
  748. * String对象内容反转<br>
  749. *
  750. * @return
  751. */
  752. public static String reverse(String oldStr) {
  753. if (oldStr == null) {
  754. return null;
  755. }
  756. StringBuffer sb = new StringBuffer(oldStr);
  757. return sb.reverse().toString();
  758. }
  759. /**
  760. * 修改索引值为index位置的字符<br>
  761. *
  762. * @return
  763. */
  764. public static String setCharAt(String oldStr, char charStr, int index) {
  765. if (oldStr == null) {
  766. return null;
  767. }
  768. StringBuffer sb = new StringBuffer(oldStr);
  769. sb.setCharAt(index, charStr);
  770. return sb.toString();
  771. }
  772. /**
  773. * 对某个字符加*操作<br>
  774. *
  775. * @return String
  776. */
  777. public static String changeTextAddsiix(String oldStr, int[] index) {
  778. String newStr = oldStr;
  779. for (int i = 0; i < index.length; i++) {
  780. if (index[i] < oldStr.length()) {
  781. newStr = setCharAt(newStr, '*', index[i]);
  782. }
  783. }
  784. return newStr;
  785. }
  786. /**
  787. * 移除所有空格<br>
  788. *
  789. * @return String
  790. */
  791. public static String replaceNull(String oldStr) {
  792. if (oldStr == null) {
  793. return null;
  794. }
  795. return oldStr.replaceAll(" ", "").replaceAll(" ", "").replaceAll("\\b", "").replaceAll("\\r", "").replaceAll("\\n", "");
  796. }
  797. /**
  798. * 移除某个字符<br>
  799. *
  800. * @return String
  801. */
  802. public static String replaceStr(String oldStr, CharSequence replaceStr) {
  803. if (oldStr == null) {
  804. return null;
  805. }
  806. return oldStr.replace(replaceStr, "");
  807. }
  808. /**
  809. * 对double类型的数据保留2位小数
  810. *
  811. * @param d
  812. * @return
  813. */
  814. public static double get2Decimal(double d) {
  815. return Math.round(d * 100) / 100d;
  816. // BigDecimal b = new BigDecimal(Double.toString(d));
  817. // return b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
  818. }
  819. /**
  820. * 对double类型的数据取整
  821. *
  822. * @param d
  823. * @return
  824. */
  825. public static double get0Decimal(double d) {
  826. String str = String.valueOf(d);
  827. String strNum = str.substring(0, str.lastIndexOf("."));//截取从字符串开始到小数点位置的字符串,就是整数部分
  828. return parseDouble(strNum);
  829. }
  830. /**
  831. * 判断一个字符串 能否转成一个Double类型的
  832. *
  833. * @param str
  834. * @return
  835. */
  836. public static boolean isDouble(String str) {
  837. try {
  838. Double.parseDouble(str);
  839. return true;
  840. } catch (NumberFormatException ex) {
  841. return false;
  842. }
  843. }
  844. /**
  845. * 字符串转成一个Double类型的
  846. *
  847. * @param str
  848. * @return
  849. */
  850. public static double parseDouble(String str) {
  851. if (isDouble(str)) {
  852. return Double.parseDouble(str);
  853. }
  854. return 0.0;
  855. }
  856. /**
  857. * 判断一个字符串 能否转成一个long类型的
  858. *
  859. * @param str
  860. * @return
  861. */
  862. public static boolean isLong(String str) {
  863. try {
  864. Long.parseLong(str);
  865. return true;
  866. } catch (NumberFormatException ex) {
  867. return false;
  868. }
  869. }
  870. /**
  871. * 字符串转成一个Long类型的
  872. *
  873. * @param str
  874. * @return
  875. */
  876. public static long parseLong(String str) {
  877. if (isLong(str)) {
  878. return Long.parseLong(str);
  879. }
  880. return 0;
  881. }
  882. /**
  883. * 判断一个字符串 能否转成一个int类型的
  884. *
  885. * @param str
  886. * @return
  887. */
  888. public static boolean isInt(String str) {
  889. try {
  890. Integer.parseInt(str);
  891. return true;
  892. } catch (NumberFormatException ex) {
  893. return false;
  894. }
  895. }
  896. /**
  897. * 字符串转成一个Int类型的
  898. *
  899. * @param str
  900. * @return
  901. */
  902. public static int parseInt(String str) {
  903. if (isInt(str)) {
  904. return Integer.parseInt(str);
  905. }
  906. return 0;
  907. }
  908. /**
  909. * 判断一个字符串 能否转成一个float类型的
  910. *
  911. * @param str
  912. * @return
  913. */
  914. public static boolean isFloat(String str) {
  915. try {
  916. Float.parseFloat(str);
  917. return true;
  918. } catch (NumberFormatException ex) {
  919. return false;
  920. }
  921. }
  922. /**
  923. * 字符串转成一个float类型的
  924. *
  925. * @param str
  926. * @return
  927. */
  928. public static float parseFloat(String str) {
  929. if (isFloat(str)) {
  930. return Float.parseFloat(str);
  931. }
  932. return 0f;
  933. }
  934. /**
  935. * 判断一个字符串 能否转成一个Double类型的
  936. *
  937. * @param str
  938. * @return
  939. */
  940. public static boolean checkDouble(String str) {
  941. Pattern pattern = Pattern.compile("^[-//+]?//d+(//.//d*)?|//.//d+$");
  942. return pattern.matcher(str).matches();
  943. }
  944. /**
  945. * String字符串的匹配(做模糊查询)
  946. *
  947. * @param objStr
  948. * @param inquiryStr
  949. * @return boolean
  950. */
  951. public static boolean vagueInquiry(String objStr, String inquiryStr) {
  952. if (null == objStr || null == inquiryStr) {
  953. return false;
  954. }
  955. Pattern p = Pattern.compile(inquiryStr);
  956. Matcher m = p.matcher(objStr);
  957. return m.find();
  958. }
  959. /**
  960. * 是否是汉字
  961. */
  962. public static boolean isChinese(String str) {
  963. String regEx = "[\u4e00-\u9fa5]";
  964. Pattern pat = Pattern.compile(regEx);
  965. Matcher matcher = pat.matcher(str);
  966. boolean flg = false;
  967. if (matcher.find())
  968. flg = true;
  969. return flg;
  970. }
  971. public boolean judgeContainsStr(String cardNum) {
  972. String regex = ".*[*]+.*";
  973. Matcher m = Pattern.compile(regex).matcher(cardNum);
  974. return m.matches();
  975. }
  976. /**
  977. * 是否为车牌号
  978. */
  979. public static boolean isCar(String carNum) {
  980. String regex = "^[京,津,渝,沪,冀,晋,辽,吉,黑,苏,浙,皖,闽,赣,鲁,豫,鄂,湘,粤,琼,川,贵,云,陕,秦,甘,陇,青,台,蒙,桂,宁,新,藏,澳,港,军,海,航,警,使,古][A-Z][0-9,A-Z]{5}$";
  981. Matcher m = Pattern.compile(regex).matcher(carNum);
  982. return m.matches();
  983. }
  984. /**
  985. * unicode码转成汉字
  986. */
  987. public static String revert(String str) {
  988. str = (str == null ? "" : str);
  989. if (str.indexOf("\\u") == -1)//如果不是unicode码则原样返回
  990. return str;
  991. StringBuffer sb = new StringBuffer(1000);
  992. for (int i = 0; i < str.length() - 6; ) {
  993. String strTemp = str.substring(i, i + 6);
  994. String value = strTemp.substring(2);
  995. int c = 0;
  996. for (int j = 0; j < value.length(); j++) {
  997. char tempChar = value.charAt(j);
  998. int t = 0;
  999. switch (tempChar) {
  1000. case 'a':
  1001. t = 10;
  1002. break;
  1003. case 'b':
  1004. t = 11;
  1005. break;
  1006. case 'c':
  1007. t = 12;
  1008. break;
  1009. case 'd':
  1010. t = 13;
  1011. break;
  1012. case 'e':
  1013. t = 14;
  1014. break;
  1015. case 'f':
  1016. t = 15;
  1017. break;
  1018. default:
  1019. t = tempChar - 48;
  1020. break;
  1021. }
  1022. c += t * ((int) Math.pow(16, (value.length() - j - 1)));
  1023. }
  1024. sb.append((char) c);
  1025. i = i + 6;
  1026. }
  1027. return sb.toString();
  1028. }
  1029. }