StringUtils.java 29 KB

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