Java常用工具类---字符编码工具类、数据类型转换工具类

偏执的太偏执、 2022-01-29 10:07 685阅读 0赞
  1. package com.jarvis.base.util;
  2. import java.io.UnsupportedEncodingException;
  3. /**
  4. * <p>
  5. * Title:字符编码工具类
  6. * </p>
  7. * <p>
  8. * Description:
  9. * </p>
  10. * <p>
  11. * Copyright: Copyright (c) 2007
  12. * </p>
  13. * <p>
  14. * Company:
  15. * </p>
  16. *
  17. * @author:
  18. * @version 1.0
  19. */
  20. public class CharTools {
  21. /**
  22. * 转换编码 ISO-8859-1到GB2312
  23. *
  24. * @param text
  25. * @return
  26. */
  27. public static final String ISO2GB(String text) {
  28. String result = "";
  29. try {
  30. result = new String(text.getBytes("ISO-8859-1"), "GB2312");
  31. } catch (UnsupportedEncodingException ex) {
  32. result = ex.toString();
  33. }
  34. return result;
  35. }
  36. /**
  37. * 转换编码 GB2312到ISO-8859-1
  38. *
  39. * @param text
  40. * @return
  41. */
  42. public static final String GB2ISO(String text) {
  43. String result = "";
  44. try {
  45. result = new String(text.getBytes("GB2312"), "ISO-8859-1");
  46. } catch (UnsupportedEncodingException ex) {
  47. ex.printStackTrace();
  48. }
  49. return result;
  50. }
  51. /**
  52. * Utf8URL编码
  53. *
  54. * @param s
  55. * @return
  56. */
  57. public static final String Utf8URLencode(String text) {
  58. StringBuffer result = new StringBuffer();
  59. for (int i = 0; i < text.length(); i++) {
  60. char c = text.charAt(i);
  61. if (c >= 0 && c <= 255) {
  62. result.append(c);
  63. } else {
  64. byte[] b = new byte[0];
  65. try {
  66. b = Character.toString(c).getBytes("UTF-8");
  67. } catch (Exception ex) {
  68. }
  69. for (int j = 0; j < b.length; j++) {
  70. int k = b[j];
  71. if (k < 0)
  72. k += 256;
  73. result.append("%" + Integer.toHexString(k).toUpperCase());
  74. }
  75. }
  76. }
  77. return result.toString();
  78. }
  79. /**
  80. * Utf8URL解码
  81. *
  82. * @param text
  83. * @return
  84. */
  85. public static final String Utf8URLdecode(String text) {
  86. String result = "";
  87. int p = 0;
  88. if (text != null && text.length() > 0) {
  89. text = text.toLowerCase();
  90. p = text.indexOf("%e");
  91. if (p == -1)
  92. return text;
  93. while (p != -1) {
  94. result += text.substring(0, p);
  95. text = text.substring(p, text.length());
  96. if (text == "" || text.length() < 9)
  97. return result;
  98. result += CodeToWord(text.substring(0, 9));
  99. text = text.substring(9, text.length());
  100. p = text.indexOf("%e");
  101. }
  102. }
  103. return result + text;
  104. }
  105. /**
  106. * utf8URL编码转字符
  107. *
  108. * @param text
  109. * @return
  110. */
  111. private static final String CodeToWord(String text) {
  112. String result;
  113. if (Utf8codeCheck(text)) {
  114. byte[] code = new byte[3];
  115. code[0] = (byte) (Integer.parseInt(text.substring(1, 3), 16) - 256);
  116. code[1] = (byte) (Integer.parseInt(text.substring(4, 6), 16) - 256);
  117. code[2] = (byte) (Integer.parseInt(text.substring(7, 9), 16) - 256);
  118. try {
  119. result = new String(code, "UTF-8");
  120. } catch (UnsupportedEncodingException ex) {
  121. result = null;
  122. }
  123. } else {
  124. result = text;
  125. }
  126. return result;
  127. }
  128. /**
  129. * 编码是否有效
  130. *
  131. * @param text
  132. * @return
  133. */
  134. @SuppressWarnings("unused")
  135. private static final boolean Utf8codeCheck(String text) {
  136. String sign = "";
  137. if (text.startsWith("%e"))
  138. for (int i = 0, p = 0; p != -1; i++) {
  139. p = text.indexOf("%", p);
  140. if (p != -1)
  141. p++;
  142. sign += p;
  143. }
  144. return sign.equals("147-1");
  145. }
  146. /**
  147. * 判断是否Utf8Url编码
  148. *
  149. * @param text
  150. * @return
  151. */
  152. public static final boolean isUtf8Url(String text) {
  153. text = text.toLowerCase();
  154. int p = text.indexOf("%");
  155. if (p != -1 && text.length() - p > 9) {
  156. text = text.substring(p, p + 9);
  157. }
  158. return Utf8codeCheck(text);
  159. }
  160. /**
  161. * 测试
  162. *
  163. * @param args
  164. */
  165. public static void main(String[] args) {
  166. // CharTools charTools = new CharTools();
  167. String url;
  168. url = "http://www.google.com/search?hl=zh-CN&newwindow=1&q=%E4%B8%AD%E5%9B%BD%E5%A4%A7%E7%99%BE%E7%A7%91%E5%9C%A8%E7%BA%BF%E5%85%A8%E6%96%87%E6%A3%80%E7%B4%A2&btnG=%E6%90%9C%E7%B4%A2&lr=";
  169. if (CharTools.isUtf8Url(url)) {
  170. System.out.println(CharTools.Utf8URLdecode(url));
  171. } else {
  172. // System.out.println(URLDecoder.decode(url));
  173. }
  174. url = "http://www.baidu.com/baidu?word=%D6%D0%B9%FA%B4%F3%B0%D9%BF%C6%D4%DA%CF%DF%C8%AB%CE%C4%BC%EC%CB%F7&tn=myie2dg";
  175. if (CharTools.isUtf8Url(url)) {
  176. System.out.println(CharTools.Utf8URLdecode(url));
  177. } else {
  178. // System.out.println(URLDecoder.decode(url));
  179. }
  180. }
  181. }
  182. package com.jarvis.base.util;
  183. import java.util.List;
  184. /**
  185. * @Title: ConvertHelper.java
  186. * @Package com.jarvis.base.util
  187. * @Description:数据类型转换
  188. * @version V1.0
  189. */
  190. public class ConvertHelper
  191. {
  192. /**
  193. * 把字串转化为整数,若转化失败,则返回0
  194. *
  195. * @param str
  196. * 字串
  197. * @return
  198. */
  199. public static int strToInt(String str) {
  200. if (str == null) {
  201. return 0;
  202. }
  203. try {
  204. return Integer.parseInt(str);
  205. } catch (Exception ex) {
  206. ex.printStackTrace();
  207. System.err.println(str + "转换成int类型失败,请检查数据来源");
  208. }
  209. return 0;
  210. }
  211. /**
  212. * 把字串转化为长整型数,若转化失败,则返回0
  213. *
  214. * @param str
  215. * 要转化为长整型的字串
  216. * @return
  217. */
  218. public static long strToLong(String str) {
  219. if (str == null) {
  220. return 0;
  221. }
  222. try {
  223. return Long.parseLong(str);
  224. } catch (Exception ex) {
  225. ex.printStackTrace();
  226. System.err.println(str + "转换成long类型失败,请检查数据来源");
  227. }
  228. return 0;
  229. }
  230. /**
  231. * 把字串转化为Float型数据,若转化失败,则返回0
  232. *
  233. * @param str
  234. * 要转化为Float的字串
  235. * @return
  236. */
  237. public static float strToFloat(String str) {
  238. if (str == null) {
  239. return 0;
  240. }
  241. try {
  242. return Float.parseFloat(str);
  243. } catch (Exception ex) {
  244. ex.printStackTrace();
  245. System.err.println(str + "转换成float类型失败,请检查数据来源");
  246. }
  247. return 0;
  248. }
  249. /**
  250. * 把字串转化为Double型数据,若转化失败,则返回0
  251. *
  252. * @param str
  253. * 要转化为Double的字串
  254. * @return
  255. */
  256. public static double strToDouble(String str) {
  257. if (str == null) {
  258. return 0;
  259. }
  260. try {
  261. return Double.parseDouble(str);
  262. } catch (Exception ex) {
  263. ex.printStackTrace();
  264. System.err.println(str + "转换成double类型失败,请检查数据来源");
  265. }
  266. return 0;
  267. }
  268. /**
  269. * 描述:字符转为一个元素的Object数组
  270. *
  271. * @param str
  272. * @return
  273. */
  274. public static Object[] strToArry(String str) {
  275. if (str == null) {
  276. return null;
  277. } else {
  278. return new Object[] { str };
  279. }
  280. }
  281. /**
  282. * 对于一个字符串数组,把字符串数组中的每一个字串转换为整数。 返回一个转换后的整型数组,对于每一个字串若转换失败,则对 应的整型值就为0
  283. *
  284. * @param strArray
  285. * 要转化的数组
  286. * @return
  287. */
  288. public static int[] strArrayToIntArray(String[] strArray) {
  289. int[] intArray = new int[strArray.length];
  290. for (int i = 0; i < strArray.length; i++) {
  291. intArray[i] = strToInt(strArray[i]);
  292. }
  293. return intArray;
  294. }
  295. /**
  296. * 描述:数组转换为字符串
  297. *
  298. * @param arg0
  299. * 数组
  300. * @return
  301. */
  302. public static String arrToString(Object[] arg0) {
  303. if (arg0 == null) {
  304. return "";
  305. }
  306. return arrToString(arg0, ",");
  307. }
  308. /**
  309. * 描述:数据转换为字符串
  310. *
  311. * @param arg0
  312. * 数组
  313. * @param arg1
  314. * 取数组个数
  315. * @return
  316. */
  317. public static String arrToString(Object[] arg0, int arg1) {
  318. if (arg0 == null) {
  319. return "";
  320. }
  321. return arrToString(arg0, ",", arg1);
  322. }
  323. /**
  324. * 描述:数据转换为字符串
  325. *
  326. * @param arg0
  327. * 数组
  328. * @param arg1
  329. * 间隔符号
  330. * @return
  331. */
  332. public static String arrToString(Object[] arg0, String arg1) {
  333. return arrToString(arg0, arg1, 0);
  334. }
  335. /**
  336. * 描述:数据转换为字符串
  337. *
  338. * @param arg0
  339. * 数组
  340. * @param arg1
  341. * 间隔符号
  342. * @param arg2
  343. * 取数组个数
  344. * @return
  345. */
  346. public static String arrToString(Object[] arg0, String arg1, int arg2) {
  347. if (arg0 == null || arg0.length == 0) {
  348. return "";
  349. } else {
  350. StringBuffer sb = new StringBuffer();
  351. int length = arg0.length;
  352. if (arg2 != 0) {
  353. length = arg2;
  354. }
  355. for (int i = 0; i < length; i++) {
  356. if (arg1 == null)
  357. arg1 = "";
  358. sb.append(arg0[i]).append(arg1);
  359. }
  360. sb.delete(sb.lastIndexOf(arg1), sb.length());
  361. return sb.toString();
  362. }
  363. }
  364. /**
  365. * 描述:List转换为字符串
  366. *
  367. * @param list
  368. * List数据
  369. * @param separation
  370. * 间隔符
  371. * @return
  372. */
  373. public static String listToString(List<?> list) {
  374. return listToString(list, ",");
  375. }
  376. /**
  377. * 描述:List转换为字符串
  378. *
  379. * @param list
  380. * List数据
  381. * @param separation
  382. * 间隔符
  383. * @return
  384. */
  385. public static String listToString(List<?> list, String separation) {
  386. return arrToString(listToStringArray(list), separation);
  387. }
  388. /**
  389. * 描述:字串数据元素包装
  390. *
  391. * @param sArr
  392. * 字串数据
  393. * @param pre
  394. * 前缀
  395. * @param aft
  396. * 后缀
  397. * @return
  398. */
  399. public static String[] strArrDoPack(String[] sArr, String pre, String aft) {
  400. return strArrDoPack(sArr, pre, aft, 1, 0);
  401. }
  402. /**
  403. * 描述:字串数据元素包装
  404. *
  405. * @param sArr
  406. * 字串数据
  407. * @param pre
  408. * 前缀
  409. * @param aft
  410. * 后缀
  411. * @param num
  412. * 生成个数
  413. * @return
  414. */
  415. public static String[] strArrDoPack(String[] sArr, String pre, String aft, int num) {
  416. return strArrDoPack(sArr, pre, aft, num, 0);
  417. }
  418. /**
  419. * 描述:字串数据元素包装
  420. *
  421. * @param sArr
  422. * 字串数据
  423. * @param pre
  424. * 前缀
  425. * @param aft
  426. * 后缀
  427. * @param num
  428. * 生成个数
  429. * @param step
  430. * 数字值1:加,-1:减,0:不变
  431. * @return
  432. */
  433. public static String[] strArrDoPack(String[] sArr, String pre, String aft, int num, int step) {
  434. String[] arr = null;
  435. if (sArr != null) {
  436. boolean isAdd = false;
  437. if (step > 0) {
  438. isAdd = true;
  439. }
  440. if (num < 0) {
  441. num = 1;
  442. }
  443. arr = new String[sArr.length * num];
  444. int icount = 0;
  445. for (int i = 0; i < num; i++) {
  446. for (int j = 0; j < sArr.length; j++) {
  447. if (StringHelper.isNotEmpty(pre)) {
  448. arr[icount] = pre + sArr[j];
  449. }
  450. if (StringHelper.isNotEmpty(aft)) {
  451. arr[icount] += aft;
  452. }
  453. icount++;
  454. }
  455. boolean b = false;
  456. if (step != 0) {
  457. pre = stepNumInStr(pre, isAdd);
  458. b = true;
  459. }
  460. if (!b) {
  461. if (step != 0) {
  462. aft = stepNumInStr(aft, isAdd);
  463. }
  464. }
  465. }
  466. }
  467. return arr;
  468. }
  469. /**
  470. * 描述:生成字符串
  471. *
  472. * @param arg0
  473. * 字符串元素
  474. * @param arg1
  475. * 生成个数
  476. * @return
  477. */
  478. public static String createStr(String arg0, int arg1) {
  479. if (arg0 == null) {
  480. return "";
  481. }
  482. return createStr(arg0, arg1, ",");
  483. }
  484. /**
  485. * 描述:生成字符串
  486. *
  487. * @param arg0
  488. * 字符串元素
  489. * @param arg1
  490. * 生成个数
  491. * @param arg2
  492. * 间隔符号
  493. * @return
  494. */
  495. public static String createStr(String arg0, int arg1, String arg2) {
  496. if (arg0 == null) {
  497. return "";
  498. } else {
  499. StringBuffer sb = new StringBuffer();
  500. for (int i = 0; i < arg1; i++) {
  501. if (arg2 == null)
  502. arg2 = "";
  503. sb.append(arg0).append(arg2);
  504. }
  505. if (sb.length() > 0) {
  506. sb.delete(sb.lastIndexOf(arg2), sb.length());
  507. }
  508. return sb.toString();
  509. }
  510. }
  511. /**
  512. * 描述:生成字符串数据
  513. *
  514. * @param arg0
  515. * 字符串元素
  516. * @param arg1
  517. * 生成个数
  518. * @param arg2
  519. * 间隔符号
  520. * @return
  521. */
  522. public static String[] createStrArr(String arg0, int arg1) {
  523. if (arg0 == null) {
  524. return null;
  525. } else {
  526. String[] arr = new String[arg1];
  527. for (int i = 0; i < arg1; i++) {
  528. arr[i] = arg0;
  529. }
  530. return arr;
  531. }
  532. }
  533. /**
  534. * 描述:只保留字符串的英文字母和“_”号
  535. *
  536. * @param str
  537. * @return
  538. */
  539. public static String replaceAllSign(String str) {
  540. if (str != null && str.length() > 0) {
  541. str = str.replaceAll("[^a-zA-Z_]", "");
  542. }
  543. return str;
  544. }
  545. /**
  546. * 描述:字串中的数字值加1
  547. *
  548. * @param str
  549. * 字串
  550. * @param isAdd
  551. * 数字值true:加,false:减
  552. * @return
  553. */
  554. public static String stepNumInStr(String str, boolean isAdd) {
  555. String sNum = str.replaceAll("[^0-9]", ",").trim();
  556. if (sNum == null || sNum.length() == 0) {
  557. return str;
  558. }
  559. String[] sNumArr = sNum.split(",");
  560. for (int i = 0; i < sNumArr.length; i++) {
  561. if (sNumArr[i] != null && sNumArr[i].length() > 0) {
  562. int itemp = Integer.parseInt(sNumArr[i]);
  563. if (isAdd) {
  564. itemp += 1;
  565. } else {
  566. itemp -= 1;
  567. }
  568. str = str.replaceFirst(sNumArr[i], String.valueOf(itemp));
  569. break;
  570. }
  571. }
  572. return str;
  573. }
  574. /**
  575. * 描述:list 转换为 String[]
  576. *
  577. * @param list
  578. * @return
  579. */
  580. public static String[] listToStringArray(List<?> list) {
  581. if (list == null || list.size() == 0) {
  582. return null;
  583. }
  584. return (String[]) list.toArray(new String[list.size()]);
  585. }
  586. }

发表评论

表情:
评论列表 (有 0 条评论,685人围观)

还没有评论,来说两句吧...

相关阅读