StringUtils.java 31 KB

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