StringUtil.java 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. package com.happy.Unitil_elc;
  2. import org.apache.commons.lang3.StringUtils;
  3. import org.apache.commons.lang3.math.NumberUtils;
  4. import java.io.UnsupportedEncodingException;
  5. import java.net.URLDecoder;
  6. import java.net.URLEncoder;
  7. import java.security.MessageDigest;
  8. import java.security.NoSuchAlgorithmException;
  9. import java.text.BreakIterator;
  10. import java.text.DecimalFormat;
  11. import java.util.*;
  12. import java.util.regex.Matcher;
  13. import java.util.regex.Pattern;
  14. public class StringUtil {
  15. /** 空字符串。 */
  16. public static final String EMPTY_STRING = "";
  17. private static final char QUOTE_ENCODE[] = """.toCharArray();
  18. private static final char AMP_ENCODE[] = "&".toCharArray();
  19. private static final char LT_ENCODE[] = "<".toCharArray();
  20. private static final char GT_ENCODE[] = ">".toCharArray();
  21. private static final int DUMP_HEX_CHAR_COUNT = 75;
  22. private static MessageDigest digest = null;
  23. // private static final int fillchar = 61;
  24. private static Random randGen = new Random();
  25. private static char numbersAndLetters[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
  26. private static final char zeroArray[] = "0000000000000000".toCharArray();
  27. private static final char[] base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();
  28. private static final char[] upcaseHexChar = "0123456789ABCDEF".toCharArray();
  29. private static final char[] lowerHexChar = "0123456789abcdef".toCharArray();
  30. private static int[] hexCharCodes = new int[256];
  31. private static int[] base64Codes = new int[256];
  32. static {
  33. for (int i = 0; i < 256; i++)
  34. hexCharCodes[i] = base64Codes[i] = -1;
  35. for (int i = 0; i < base64Chars.length; i++)
  36. base64Codes[base64Chars[i]] = (byte) i;
  37. for (int i = 0; i < upcaseHexChar.length; i++)
  38. hexCharCodes[upcaseHexChar[i]] = (byte) i;
  39. for (int i = 0; i < lowerHexChar.length; i++)
  40. hexCharCodes[lowerHexChar[i]] = (byte) i;
  41. }
  42. /**
  43. * 如果字符串是<code>null</code>,则返回指定默认字符串,否则返回字符串本身。
  44. *
  45. * <pre>
  46. * StringUtil.defaultIfNull(null, &quot;default&quot;) = &quot;default&quot;
  47. * StringUtil.defaultIfNull(&quot;&quot;, &quot;default&quot;) = &quot;&quot;
  48. * StringUtil.defaultIfNull(&quot; &quot;, &quot;default&quot;) = &quot; &quot;
  49. * StringUtil.defaultIfNull(&quot;bat&quot;, &quot;default&quot;) = &quot;bat&quot;
  50. * </pre>
  51. *
  52. * @param str 要转换的字符串
  53. * @param defaultStr 默认字符串
  54. * @return 字符串本身或指定的默认字符串
  55. */
  56. public static String defaultIfNull(String str, String defaultStr) {
  57. return (str == null) ? defaultStr : str;
  58. }
  59. public static boolean isDigits(String str) {
  60. if ((str == null) || (str.length() == 0)) {
  61. return false;
  62. }
  63. for (int i = 0; i < str.length(); i++) {
  64. if (!Character.isDigit(str.charAt(i))) {
  65. return false;
  66. }
  67. }
  68. return true;
  69. }
  70. public static boolean isDigits(Object obj) {
  71. if (obj == null) return false;
  72. String str = obj.toString();
  73. return isDigits(str);
  74. }
  75. public static final String escapeHTMLTags(String in) {
  76. if (in == null) return null;
  77. int i = 0;
  78. int last = 0;
  79. char input[] = in.toCharArray();
  80. int len = input.length;
  81. StringBuffer out = new StringBuffer((int) ((double) len * 1.3D));
  82. for (; i < len; i++) {
  83. char ch = input[i];
  84. if (ch > '>') continue;
  85. if (ch == '<') {
  86. if (i > last) out.append(input, last, i - last);
  87. last = i + 1;
  88. out.append(LT_ENCODE);
  89. continue;
  90. }
  91. if (ch != '>') continue;
  92. if (i > last) out.append(input, last, i - last);
  93. last = i + 1;
  94. out.append(GT_ENCODE);
  95. }
  96. if (last == 0) return in;
  97. if (i > last) out.append(input, last, i - last);
  98. return out.toString();
  99. }
  100. public static final synchronized String hash(String data) {
  101. if (digest == null) try {
  102. digest = MessageDigest.getInstance("MD5");
  103. } catch (NoSuchAlgorithmException nsae) {
  104. System.err.println("Failed to load the MD5 MessageDigest. We will be unable to function normally.");
  105. nsae.printStackTrace();
  106. }
  107. digest.update(data.getBytes());
  108. return encodeHex(digest.digest());
  109. }
  110. public static final String encodeHex(byte bytes[]) {
  111. char[] buf = new char[bytes.length * 2];
  112. for (int i = 0; i < bytes.length; i++) {
  113. int code = bytes[i] & 0xff;
  114. buf[2 * i] = upcaseHexChar[code >> 4];
  115. buf[2 * i + 1] = upcaseHexChar[code & 0xf];
  116. }
  117. return new String(buf);
  118. }
  119. public static final String dumpHex(byte bytes[]) {
  120. int bytepos, bufpos, linecount = (bytes.length + 15) / 16;
  121. char[] buf = new char[linecount * DUMP_HEX_CHAR_COUNT];
  122. byte[] bs = new byte[16];
  123. bytepos = 0;
  124. for (int i = 0; i < linecount; i++) {
  125. int addr = i * 16;
  126. bufpos = i * DUMP_HEX_CHAR_COUNT;
  127. buf[bufpos++] = upcaseHexChar[(addr >> 12) & 0xf];
  128. buf[bufpos++] = upcaseHexChar[(addr >> 8) & 0xf];
  129. buf[bufpos++] = upcaseHexChar[(addr >> 4) & 0xf];
  130. buf[bufpos++] = upcaseHexChar[(addr) & 0xf];
  131. buf[bufpos++] = ' ';
  132. buf[bufpos++] = ' ';
  133. for (int j = 0; j < 16; j++, bytepos++) {
  134. if (bytepos < bytes.length) {
  135. int code = bytes[bytepos] & 0xff;
  136. bs[j] = bytes[bytepos];
  137. buf[bufpos++] = upcaseHexChar[code >> 4];
  138. buf[bufpos++] = upcaseHexChar[code & 0xf];
  139. if (j == 7) buf[bufpos++] = '-';
  140. else buf[bufpos++] = ' ';
  141. } else {
  142. buf[bufpos++] = ' ';
  143. buf[bufpos++] = ' ';
  144. buf[bufpos++] = ' ';
  145. bs[j] = ' ';
  146. }
  147. }
  148. buf[bufpos++] = ' ';
  149. buf[bufpos++] = ' ';
  150. char[] chs = new String(bs).toCharArray();
  151. for (int k = 0; k < chs.length; k++) {
  152. char ch = chs[k];
  153. if ((ch > '\0') && (ch < ' ')) buf[bufpos++] = '.';
  154. else buf[bufpos++] = ch;
  155. }
  156. int endLine = (i + 1) * DUMP_HEX_CHAR_COUNT - 2;
  157. for (; bufpos < endLine; bufpos++)
  158. buf[bufpos] = ' ';
  159. buf[bufpos++] = '\r';
  160. buf[bufpos++] = '\n';
  161. }
  162. return new String(buf);
  163. }
  164. public static final byte[] decodeHex(String hex) {
  165. char chars[] = hex.toCharArray();
  166. byte bytes[] = new byte[chars.length / 2];
  167. int byteCount = 0;
  168. for (int i = 0; i < chars.length; i += 2) {
  169. byte newByte = 0;
  170. newByte |= hexCharCodes[chars[i]];
  171. newByte <<= 4;
  172. newByte |= hexCharCodes[chars[i + 1]];
  173. bytes[byteCount] = newByte;
  174. byteCount++;
  175. }
  176. return bytes;
  177. }
  178. public static String encodeBase64(String data) {
  179. return encodeBase64(data.getBytes());
  180. }
  181. public static String encodeBase64(byte data[]) {
  182. int len = data.length;
  183. StringBuffer ret = new StringBuffer((len / 3 + 1) * 4);
  184. for (int i = 0; i < len; i++) {
  185. int c = data[i] >> 2 & 0x3f;
  186. ret.append(base64Chars[c]);
  187. c = data[i] << 4 & 0x3f;
  188. if (++i < len) c |= data[i] >> 4 & 0xf;
  189. ret.append(base64Chars[c]);
  190. if (i < len) {
  191. c = data[i] << 2 & 0x3f;
  192. if (++i < len) c |= data[i] >> 6 & 3;
  193. ret.append(base64Chars[c]);
  194. } else {
  195. i++;
  196. ret.append('=');
  197. }
  198. if (i < len) {
  199. c = data[i] & 0x3f;
  200. ret.append(base64Chars[c]);
  201. } else {
  202. ret.append('=');
  203. }
  204. }
  205. return ret.toString();
  206. }
  207. public static byte[] decodeBase64(String data) {
  208. int len = data.length();
  209. byte[] result = new byte[((len * 3) / 4)];
  210. int pos = 0;
  211. for (int i = 0; i < len; i++) {
  212. int c = base64Codes[data.charAt(i)];
  213. i++;
  214. int c1 = base64Codes[data.charAt(i)];
  215. c = c << 2 | c1 >> 4 & 3;
  216. result[pos++] = (byte) c;
  217. if (++i < len) {
  218. c = data.charAt(i);
  219. if (61 == c) break;
  220. c = base64Codes[data.charAt(i)];
  221. c1 = c1 << 4 & 0xf0 | c >> 2 & 0xf;
  222. result[pos++] = (byte) c1;
  223. }
  224. if (++i >= len) continue;
  225. c1 = data.charAt(i);
  226. if (61 == c1) break;
  227. c1 = base64Codes[data.charAt(i)];
  228. c = c << 6 & 0xc0 | c1;
  229. result[pos++] = (byte) c;
  230. }
  231. if (result.length != pos) {
  232. byte[] result2 = new byte[pos];
  233. System.arraycopy(result, 0, result2, 0, pos);
  234. result = result2;
  235. }
  236. return result;
  237. }
  238. public static final String[] toLowerCaseWordArray(String text) {
  239. if (text == null || text.length() == 0) return new String[0];
  240. List<String> wordList = new ArrayList<String>();
  241. BreakIterator boundary = BreakIterator.getWordInstance();
  242. boundary.setText(text);
  243. int start = 0;
  244. for (int end = boundary.next(); end != -1; end = boundary.next()) {
  245. String tmp = text.substring(start, end).trim();
  246. tmp = tmp.replace("+", "");
  247. tmp = tmp.replace("/", "");
  248. tmp = tmp.replace("\\", "");
  249. tmp = tmp.replace("#", "");
  250. tmp = tmp.replace("*", "");
  251. tmp = tmp.replace(")", "");
  252. tmp = tmp.replace("(", "");
  253. tmp = tmp.replace("&", "");
  254. if (tmp.length() > 0) wordList.add(tmp);
  255. start = end;
  256. }
  257. return (String[]) (String[]) wordList.toArray(new String[wordList.size()]);
  258. }
  259. public static final String createTempTable() {
  260. return "temp_" + StringUtil.randomString(10);
  261. }
  262. /**
  263. * 产生指定长度的字符串
  264. *
  265. * @param length
  266. * @return
  267. */
  268. public static final String randomString(int length) {
  269. if (length < 1) {
  270. return null;
  271. }
  272. char randBuffer[] = new char[length];
  273. for (int i = 0; i < randBuffer.length; i++)
  274. randBuffer[i] = numbersAndLetters[randGen.nextInt(numbersAndLetters.length)];
  275. return new String(randBuffer);
  276. }
  277. public static final String randomNumber(int length) {
  278. Long value = Math.abs(randGen.nextLong());
  279. String strValue = value.toString();
  280. if (strValue.length() > length) {
  281. strValue = strValue.substring(0, length);
  282. }
  283. return strValue;
  284. }
  285. public static final String chopAtWord(String string, int length) {
  286. if (string == null) return string;
  287. char charArray[] = string.toCharArray();
  288. int sLength = string.length();
  289. if (length < sLength) sLength = length;
  290. for (int i = 0; i < sLength - 1; i++) {
  291. if (charArray[i] == '\r' && charArray[i + 1] == '\n') return string.substring(0, i + 1);
  292. if (charArray[i] == '\n') return string.substring(0, i);
  293. }
  294. if (charArray[sLength - 1] == '\n') return string.substring(0, sLength - 1);
  295. if (string.length() < length) return string;
  296. for (int i = length - 1; i > 0; i--)
  297. if (charArray[i] == ' ') return string.substring(0, i).trim();
  298. return string.substring(0, length);
  299. }
  300. public static final String escapeForXML(String string) {
  301. if (string == null) return null;
  302. int i = 0;
  303. int last = 0;
  304. char input[] = string.toCharArray();
  305. int len = input.length;
  306. StringBuffer out = new StringBuffer((int) ((double) len * 1.3D));
  307. for (; i < len; i++) {
  308. char ch = input[i];
  309. if (ch > '>') continue;
  310. if (ch == '<') {
  311. if (i > last) out.append(input, last, i - last);
  312. last = i + 1;
  313. out.append(LT_ENCODE);
  314. continue;
  315. }
  316. if (ch == '&') {
  317. if (i > last) out.append(input, last, i - last);
  318. last = i + 1;
  319. out.append(AMP_ENCODE);
  320. continue;
  321. }
  322. if (ch != '"') continue;
  323. if (i > last) out.append(input, last, i - last);
  324. last = i + 1;
  325. out.append(QUOTE_ENCODE);
  326. }
  327. if (last == 0) return string;
  328. if (i > last) out.append(input, last, i - last);
  329. return out.toString();
  330. }
  331. public static final String unescapeFromXML(String string) {
  332. string = string.replace("&lt;", "<");
  333. string = string.replace("&gt;", ">");
  334. string = string.replace("&quot;", "\"");
  335. return string.replace("&amp;", "&");
  336. }
  337. public static final String zeroPadString(String string, int length) {
  338. if (string == null || string.length() > length) {
  339. return string;
  340. } else {
  341. StringBuffer buf = new StringBuffer(length);
  342. buf.append(zeroArray, 0, length - string.length()).append(string);
  343. return buf.toString();
  344. }
  345. }
  346. public static final String zeroAppendString(String string, int length) {
  347. if (string == null || string.length() > length) {
  348. return string;
  349. } else {
  350. StringBuffer buf = new StringBuffer(length);
  351. buf.append(string).append(zeroArray, 0, length - string.length());
  352. return buf.toString();
  353. }
  354. }
  355. public static final String charPadString(String string, int length, char append) {
  356. if (string == null || string.length() > length) {
  357. return string;
  358. } else {
  359. char[] charArray = new char[length - string.length()];
  360. for (int i = 0; i < charArray.length; i++) {
  361. charArray[i] = append;
  362. }
  363. StringBuffer buf = new StringBuffer(length);
  364. buf.append(charArray, 0, charArray.length).append(string);
  365. return buf.toString();
  366. }
  367. }
  368. public static final String charAppendString(String string, int length, char append) {
  369. if (string == null || string.length() > length) {
  370. return string;
  371. } else {
  372. char[] charArray = new char[length - string.length()];
  373. for (int i = 0; i < charArray.length; i++) {
  374. charArray[i] = append;
  375. }
  376. StringBuffer buf = new StringBuffer(length);
  377. buf.append(string).append(charArray, 0, charArray.length);
  378. return buf.toString();
  379. }
  380. }
  381. public static final String dateToMillis(Date date) {
  382. return zeroPadString(Long.toString(date.getTime()), 15);
  383. }
  384. public static final String collectionToString(Collection<Object> c, String split) {
  385. StringBuilder ret = new StringBuilder();
  386. List<Object> a;
  387. if (c == null) return null;
  388. if (split == null) return null;
  389. a = new ArrayList<Object>(c);
  390. try {
  391. for (int i = 0; i < a.size(); i++) {
  392. String t = (String) a.get(i);
  393. if (i == a.size() - 1) {
  394. ret.append(t);
  395. } else {
  396. ret.append(t).append(split);
  397. }
  398. }
  399. return ret.toString();
  400. } catch (Exception e) {
  401. e.printStackTrace();
  402. }
  403. return null;
  404. }
  405. public static String encodeUrlString(String str) {
  406. String strret = null;
  407. if (str == null) return str;
  408. try {
  409. strret = URLEncoder.encode(str, "GBK");
  410. } catch (Exception e) {
  411. e.printStackTrace(System.err);
  412. return null;
  413. }
  414. return strret;
  415. }
  416. public static String decodeUrlString(String str) {
  417. String strret = null;
  418. if (str == null) return str;
  419. try {
  420. strret = URLDecoder.decode(str, "GBK");
  421. } catch (Exception e) {
  422. e.printStackTrace(System.err);
  423. return null;
  424. }
  425. return strret;
  426. }
  427. public static String join(Object list[], String separator) {
  428. int listSize = list.length;
  429. int bufSize = listSize == 0 ? 0 : (list[0].toString().length() + separator.length()) * listSize;
  430. StringBuffer buf = new StringBuffer(bufSize);
  431. for (int i = 0; i < listSize; i++) {
  432. if (i > 0) buf.append(separator);
  433. buf.append(list[i]);
  434. }
  435. return buf.toString();
  436. }
  437. public static String join(Iterator<?> iterator, String separator) {
  438. StringBuffer buf = new StringBuffer();
  439. do {
  440. if (!iterator.hasNext()) break;
  441. buf.append(iterator.next());
  442. if (iterator.hasNext()) buf.append(separator);
  443. } while (true);
  444. return buf.toString();
  445. }
  446. public static boolean isEmpty(String param) {
  447. return param == null || "".equals(param) || "".equals(param.trim()) || "null".equals(param) || "\"null\"".equals(param) || "undefined".equals(param) || "[]".equals(param);
  448. }
  449. public static boolean isNotEmpty(String param) {
  450. return !isEmpty(param);
  451. }
  452. public static int getDecimalplace(String factor) {
  453. int pointPos = factor.indexOf(".");
  454. int onePos = factor.indexOf("1");
  455. int precision = onePos - pointPos;
  456. return precision > 0 ? precision : 0;
  457. }
  458. /**
  459. * 对字符串做定长处理,超过长度,截取,长度不够补在前方补齐
  460. *
  461. * @param str
  462. * @param len
  463. * @param appendStr 补齐的字符,
  464. * @return
  465. */
  466. public static String AddjustLength(String str, int len, String appendStr) {
  467. if (str == null) {
  468. return null;
  469. }
  470. if (str.length() == len) {
  471. return str;
  472. } else if (str.length() < len) {
  473. StringBuffer buf = new StringBuffer(len);
  474. for (int i = 0; i < len - str.length(); i++) {
  475. buf.append(appendStr);
  476. }
  477. buf.append(str);
  478. if (buf.length() > len) {
  479. return buf.substring(buf.length() - len);
  480. } else {
  481. return buf.toString();
  482. }
  483. } else {
  484. return str.substring(str.length() - len, str.length());
  485. }
  486. }
  487. /**
  488. * 比较separator分隔的2个字符串是否相等
  489. *
  490. * @param str1
  491. * @param str2
  492. * @param separator 字符串分隔符
  493. * @return
  494. */
  495. public static boolean isEquals(String str1, String str2, String separator) {
  496. if (StringUtils.isEmpty(str1) && StringUtils.isEmpty(str2)) {
  497. return false;
  498. }
  499. String[] str1Array = StringUtils.split(str1, separator);
  500. String[] str2Array = StringUtils.split(str2, separator);
  501. if (str1Array.length != str2Array.length) {
  502. return false;
  503. }
  504. List<String> refList = new ArrayList<String>();
  505. for (String refStr : str1Array) {
  506. refList.add(refStr);
  507. }
  508. for (String desStr : str2Array) {
  509. if (!refList.contains(desStr)) {
  510. return false;
  511. }
  512. }
  513. return true;
  514. }
  515. /**
  516. * 转换字符串分隔的字符串为Integer数组
  517. *
  518. * @param values
  519. * @param spare
  520. * @return
  521. */
  522. public static List<Integer> parseIntList(String values, String spare) {
  523. List<Integer> list = new ArrayList<Integer>();
  524. String[] valueArray = StringUtils.split(values, spare);
  525. if (valueArray != null) {
  526. for (String value : valueArray) {
  527. if (NumberUtils.isDigits(value)) {
  528. list.add(NumberUtils.toInt(value));
  529. }
  530. }
  531. }
  532. return list;
  533. }
  534. public static double parseDouble(String src) {
  535. if (!isEmpty(src)) {
  536. return Double.parseDouble(src);
  537. }
  538. return 0;
  539. }
  540. public static int parseInt(String src) {
  541. if (!isEmpty(src)) {
  542. return Integer.parseInt(src);
  543. }
  544. return 0;
  545. }
  546. public static long parseLong(String src) {
  547. if (!isEmpty(src)) {
  548. return Long.parseLong(src);
  549. }
  550. return 0;
  551. }
  552. public static String format(String number, int decimalDigits) {
  553. if (!StringUtil.isEmpty(number) && NumberUtils.isDigits(number)) {
  554. return StringUtil.format(Double.parseDouble(number), decimalDigits);
  555. } else {
  556. return number;
  557. }
  558. }
  559. public static String trim(String src) {
  560. return src == null ? "" : src.trim();
  561. }
  562. /**
  563. * 数字格式化函数
  564. *
  565. * @param number : 格式化前的数字;
  566. * @param decimalDigits : 小数位数;
  567. * @return: 字符串;
  568. */
  569. public static String format(double number, int decimalDigits) {
  570. if (number == 0d) {
  571. number = 0d;
  572. }
  573. boolean flag = false;
  574. if (decimalDigits < 0) {
  575. // 小数位数不能小于0.
  576. return "";
  577. }
  578. String pattern = "##################";
  579. if (decimalDigits > 0) {
  580. flag = true;
  581. pattern += ".";
  582. for (int i = 0; i < decimalDigits; i++) {
  583. pattern += "0";
  584. }
  585. }
  586. DecimalFormat df = new DecimalFormat(pattern);
  587. if (number <= -1d) {
  588. return df.format(number);
  589. } else if (number > -1d && number < 0d) {
  590. return "-0" + df.format(number).substring(1);
  591. } else if (number >= 0d && number < 1d) {
  592. if (flag) {
  593. String temp = df.format(number);
  594. if (StringUtil.isNotEmpty(temp) && temp.substring(0, 1).equals(".")) {
  595. return "0" + df.format(number);
  596. } else {
  597. return temp;
  598. }
  599. } else {
  600. return df.format(number);
  601. }
  602. } else {
  603. return df.format(number);
  604. }
  605. }
  606. public static Integer toInteger(Object object) {
  607. if (object == null) {
  608. return 0;
  609. }
  610. return Integer.valueOf(object.toString());
  611. }
  612. public static Short toShort(Object object) {
  613. if (object == null) {
  614. return null;
  615. }
  616. return Short.valueOf(object.toString());
  617. }
  618. public static String objectToString(Object obj) {
  619. if (obj == null) {
  620. return null;
  621. } else {
  622. return obj.toString();
  623. }
  624. }
  625. /**
  626. * 字串逆排
  627. *
  628. * @return
  629. */
  630. public static String revert(String src) {
  631. if (isEmpty(src)) return "";
  632. StringBuffer result = new StringBuffer();
  633. for (int i = src.length() - 1; i >= 0; i--) {
  634. result.append(src.charAt(i));
  635. }
  636. return result.toString();
  637. }
  638. public static boolean isValidateString(String path) {
  639. if (StringUtil.isEmpty(path)) {
  640. return false;
  641. } else {
  642. return true;
  643. }
  644. }
  645. public static String encodingUtf8(String src) throws UnsupportedEncodingException {
  646. if (isEmpty(src)) return "";
  647. return new String(src.getBytes("ISO-8859-1"), "UTF-8");
  648. }
  649. public static String encodingJson(String src) throws UnsupportedEncodingException {
  650. if (isEmpty(src)) return "";
  651. int pos = src.indexOf("\\u");
  652. StringBuffer sb = new StringBuffer();
  653. while (pos >= 0 && (pos + 6 <= src.length())) {
  654. sb.append(src.substring(0, pos));
  655. String temp = src.substring(pos + 2, pos + 6);
  656. sb.append((char) Integer.parseInt(temp, 16));// 将\u6d4b\u8bd5
  657. // 格式的文字转为
  658. // 正常的string编码
  659. // “测试”(因为Ext.encode(array)的方法解析出来是jsonarray格式)
  660. src = src.substring(pos + 6);
  661. pos = src.indexOf("\\u");
  662. }
  663. sb.append(src);
  664. return sb.toString();
  665. }
  666. /**
  667. * 正则
  668. */
  669. public static String replaceBlank(String str) {
  670. String dest = "";
  671. if (str != null) {
  672. Pattern p = Pattern.compile("\\s*|\t|\r|\n");
  673. Matcher m = p.matcher(str);
  674. dest = m.replaceAll("");
  675. }
  676. return dest;
  677. }
  678. /**
  679. * 不定参数校验空
  680. *
  681. * @param args
  682. * @return
  683. */
  684. public static boolean isNull(String... args) {
  685. for (String arg : args) {
  686. if (StringUtils.isEmpty(arg)) {
  687. return true;
  688. }
  689. }
  690. return false;
  691. }
  692. /**
  693. * 不定参数校验数字
  694. *
  695. * @param args
  696. * @return
  697. */
  698. public static boolean isNotNumber(String... args) {
  699. for (String arg : args) {
  700. if (!NumberUtils.isDigits(arg)) {
  701. return true;
  702. }
  703. }
  704. return false;
  705. }
  706. /**
  707. * 不定参数校验数字
  708. *
  709. * @param args
  710. * @return
  711. */
  712. public static boolean isNotDigits(String... args) {
  713. for (String arg : args) {
  714. if (!NumberUtils.isDigits(arg)) {
  715. return true;
  716. }
  717. }
  718. return false;
  719. }
  720. /**
  721. * toString增加判空
  722. */
  723. public static String toString(Object obj) {
  724. if(obj == null) {
  725. return "";
  726. }
  727. return obj.toString();
  728. }
  729. /**
  730. * @param value:十进制字字符
  731. * @param radix:目标进制
  732. * @param length:目标长度
  733. * @param pad:前补的字符
  734. * @return
  735. */
  736. public static String numStr2PadString(String value, int radix, int length, String pad) {
  737. if (value == null) return charPad("", length, pad);
  738. // Integer temp = NumberUtils.toInt(value);
  739. String result = Integer.toString(NumberUtils.toInt(value), radix);
  740. return AddjustLength(result, length, pad);
  741. }
  742. public static String charPad(String value, int length, String pad) {
  743. if (value == null || value.length() > length) return value;
  744. String padString = "";
  745. for (int i = 0; i < (length - value.length()); i++) {
  746. padString += pad;
  747. }
  748. return padString + value;
  749. }
  750. public static void main(String[] args) {
  751. System.out.println("operator_secret: " + randomString(17));
  752. System.out.println("data_secret: " + randomString(16));
  753. System.out.println("data_secret_iv: " + randomString(16));
  754. System.out.println("sign_secret: " + randomString(19));
  755. }
  756. }