PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/src/com/cyh/StringUtil.java

http://cyhjason.googlecode.com/
Java | 540 lines | 341 code | 33 blank | 166 comment | 107 complexity | 962018e0a5e3d28fa00985ef9a401202 MD5 | raw file
  1. package com.cyh;
  2. import java.util.ArrayList;
  3. import java.util.LinkedHashSet;
  4. import java.util.Set;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7. /**
  8. * ?????????????????
  9. *
  10. * @author Administrator
  11. * @Date Jul 18, 2008
  12. * @Time 2:19:47 PM
  13. * @version 1.0
  14. */
  15. public class StringUtil {
  16. /**
  17. * ??????????
  18. *
  19. * @param str
  20. * String ?????
  21. * @param splitsign
  22. * String ???
  23. * @return String[] ?????????
  24. */
  25. @SuppressWarnings("unchecked")
  26. public static String[] split(String str, String splitsign) {
  27. int index;
  28. if (str == null || splitsign == null) {
  29. return null;
  30. }
  31. ArrayList al = new ArrayList();
  32. while ((index = str.indexOf(splitsign)) != -1) {
  33. al.add(str.substring(0, index));
  34. str = str.substring(index + splitsign.length());
  35. }
  36. al.add(str);
  37. return (String[]) al.toArray(new String[0]);
  38. }
  39. /**
  40. * ??????????
  41. *
  42. * @param from
  43. * String ?????
  44. * @param to
  45. * String ?????
  46. * @param source
  47. * String ????
  48. * @return String ???????
  49. */
  50. public static String replace(String from, String to, String source) {
  51. if (source == null || from == null || to == null)
  52. return null;
  53. StringBuffer str = new StringBuffer("");
  54. int index = -1;
  55. while ((index = source.indexOf(from)) != -1) {
  56. str.append(source.substring(0, index) + to);
  57. source = source.substring(index + from.length());
  58. index = source.indexOf(from);
  59. }
  60. str.append(source);
  61. return str.toString();
  62. }
  63. /**
  64. * ??????????HTML???????(?????????)
  65. *
  66. * @param str
  67. * String ?????
  68. * @return String ???????
  69. */
  70. public static String htmlencode(String str) {
  71. if (str == null) {
  72. return null;
  73. }
  74. return replace("\"", "&quot;", replace("<", "&lt;", str));
  75. }
  76. /**
  77. * ?????????????????????????????
  78. *
  79. * @param str
  80. * String
  81. * @return String
  82. */
  83. public static String htmldecode(String str) {
  84. if (str == null) {
  85. return null;
  86. }
  87. return replace("&quot;", "\"", replace("&lt;", "<", str));
  88. }
  89. private static final String _BR = "<br/>";
  90. /**
  91. * ??????????????????????????????TAB
  92. *
  93. * @param str
  94. * String ?????
  95. * @return String ???????
  96. */
  97. public static String htmlshow(String str) {
  98. if (str == null) {
  99. return null;
  100. }
  101. str = replace("<", "&lt;", str);
  102. str = replace(" ", "&nbsp;", str);
  103. str = replace("\r\n", _BR, str);
  104. str = replace("\n", _BR, str);
  105. str = replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;", str);
  106. return str;
  107. }
  108. /**
  109. * ?????????????????
  110. *
  111. * @param str
  112. * String ???
  113. * @param length
  114. * int ????
  115. * @return String ??????
  116. */
  117. public static String toLength(String str, int length) {
  118. if (str == null) {
  119. return null;
  120. }
  121. if (length <= 0) {
  122. return "";
  123. }
  124. try {
  125. if (str.getBytes("GBK").length <= length) {
  126. return str;
  127. }
  128. } catch (Exception e) {
  129. }
  130. StringBuffer buff = new StringBuffer();
  131. int index = 0;
  132. char c;
  133. length -= 3;
  134. while (length > 0) {
  135. c = str.charAt(index);
  136. if (c < 128) {
  137. length--;
  138. } else {
  139. length--;
  140. length--;
  141. }
  142. buff.append(c);
  143. index++;
  144. }
  145. buff.append("...");
  146. return buff.toString();
  147. }
  148. /**
  149. * ????????????
  150. *
  151. * @param str
  152. * ??????
  153. * @return ?????true,????false
  154. */
  155. public static boolean isInteger(String str) {
  156. Pattern pattern = Pattern.compile("^[-\\+]?[\\d]+$");
  157. return pattern.matcher(str).matches();
  158. }
  159. /**
  160. * ???????????double?float
  161. *
  162. * @param str
  163. * ??????
  164. * @return ??????true,????false
  165. */
  166. public static boolean isDouble(String str) {
  167. Pattern pattern = Pattern.compile("^[-\\+]?\\d+\\.\\d+$");
  168. return pattern.matcher(str).matches();
  169. }
  170. /**
  171. * ????????? c ??????
  172. */
  173. public static boolean isLetter(String str) {
  174. if (str == null || str.length() < 0) {
  175. return false;
  176. }
  177. Pattern pattern = Pattern.compile("[\\w\\.-_]*");
  178. return pattern.matcher(str).matches();
  179. }
  180. /**
  181. * ??????????Email content ??????
  182. *
  183. * @param content
  184. * @return
  185. */
  186. public static String parse(String content) {
  187. String email = null;
  188. if (content == null || content.length() < 1) {
  189. return email;
  190. }
  191. // ????@
  192. int beginPos;
  193. int i;
  194. String token = "@";
  195. String preHalf = "";
  196. String sufHalf = "";
  197. beginPos = content.indexOf(token);
  198. if (beginPos > -1) {
  199. // ????
  200. String s = null;
  201. i = beginPos;
  202. while (i > 0) {
  203. s = content.substring(i - 1, i);
  204. if (isLetter(s))
  205. preHalf = s + preHalf;
  206. else
  207. break;
  208. i--;
  209. }
  210. // ????
  211. i = beginPos + 1;
  212. while (i < content.length()) {
  213. s = content.substring(i, i + 1);
  214. if (isLetter(s))
  215. sufHalf = sufHalf + s;
  216. else
  217. break;
  218. i++;
  219. }
  220. // ?????
  221. email = preHalf + "@" + sufHalf;
  222. if (isEmail(email)) {
  223. return email;
  224. }
  225. }
  226. return null;
  227. }
  228. /**
  229. * ?????????????????Email??.
  230. *
  231. * @param str
  232. * ??????
  233. * @return ?Email????true,????false
  234. */
  235. public static boolean isEmail(String email) {
  236. if (email == null || email.length() < 1 || email.length() > 256) {
  237. return false;
  238. }
  239. Pattern pattern = Pattern
  240. .compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
  241. return pattern.matcher(email).matches();
  242. }
  243. /**
  244. * ???????????????????
  245. *
  246. * @param str
  247. * ??????
  248. * @return ????????true,????false
  249. */
  250. public static boolean isChinese(String str) {
  251. Pattern pattern = Pattern.compile("[\u0391-\uFFE5]+$");
  252. return pattern.matcher(str).matches();
  253. }
  254. /**
  255. * ??????????,??null?""
  256. *
  257. * @param str
  258. * @return
  259. */
  260. public static boolean isBlank(String str) {
  261. return str == null || str.trim().length() == 0;
  262. }
  263. /**
  264. * ????????????
  265. *
  266. * @param x
  267. * @return
  268. */
  269. public static boolean isPrime(int x) {
  270. if (x <= 7) {
  271. if (x == 2 || x == 3 || x == 5 || x == 7)
  272. return true;
  273. }
  274. int c = 7;
  275. if (x % 2 == 0)
  276. return false;
  277. if (x % 3 == 0)
  278. return false;
  279. if (x % 5 == 0)
  280. return false;
  281. int end = (int) Math.sqrt(x);
  282. while (c <= end) {
  283. if (x % c == 0) {
  284. return false;
  285. }
  286. c += 4;
  287. if (x % c == 0) {
  288. return false;
  289. }
  290. c += 2;
  291. if (x % c == 0) {
  292. return false;
  293. }
  294. c += 4;
  295. if (x % c == 0) {
  296. return false;
  297. }
  298. c += 2;
  299. if (x % c == 0) {
  300. return false;
  301. }
  302. c += 4;
  303. if (x % c == 0) {
  304. return false;
  305. }
  306. c += 6;
  307. if (x % c == 0) {
  308. return false;
  309. }
  310. c += 2;
  311. if (x % c == 0) {
  312. return false;
  313. }
  314. c += 6;
  315. }
  316. return true;
  317. }
  318. /**
  319. * ????????????
  320. *
  321. * @param str
  322. * ?????
  323. * @return String ?????????????
  324. */
  325. public static String hangeToBig(String str) {
  326. double value;
  327. try {
  328. value = Double.parseDouble(str.trim());
  329. } catch (Exception e) {
  330. return null;
  331. }
  332. char[] hunit = { '?', '?', '?' }; // ??????
  333. char[] vunit = { '?', '?' }; // ????
  334. char[] digit = { '?', '?', '?', '?', '?', '?', '?', '?', '?', '?' }; // ????
  335. long midVal = (long) (value * 100); // ?????
  336. String valStr = String.valueOf(midVal); // ??????
  337. String head = valStr.substring(0, valStr.length() - 2); // ?????
  338. String rail = valStr.substring(valStr.length() - 2); // ?????
  339. String prefix = ""; // ?????????
  340. String suffix = ""; // ?????????
  341. // ?????????
  342. if (rail.equals("00")) { // ???????0
  343. suffix = "?";
  344. } else {
  345. suffix = digit[rail.charAt(0) - '0'] + "?"
  346. + digit[rail.charAt(1) - '0'] + "?"; // ?????????
  347. }
  348. // ?????????
  349. char[] chDig = head.toCharArray(); // ????????????
  350. char zero = '0'; // ??'0'?????0
  351. byte zeroSerNum = 0; // ????0???
  352. for (int i = 0; i < chDig.length; i++) { // ????????
  353. int idx = (chDig.length - i - 1) % 4; // ?????
  354. int vidx = (chDig.length - i - 1) / 4; // ????
  355. if (chDig[i] == '0') { // ???????0
  356. zeroSerNum++; // ??0????
  357. if (zero == '0') { // ??
  358. zero = digit[0];
  359. } else if (idx == 0 && vidx > 0 && zeroSerNum < 4) {
  360. prefix += vunit[vidx - 1];
  361. zero = '0';
  362. }
  363. continue;
  364. }
  365. zeroSerNum = 0; // ??0????
  366. if (zero != '0') { // ??????0,???,???,????
  367. prefix += zero;
  368. zero = '0';
  369. }
  370. prefix += digit[chDig[i] - '0']; // ???????
  371. if (idx > 0)
  372. prefix += hunit[idx - 1];
  373. if (idx == 0 && vidx > 0) {
  374. prefix += vunit[vidx - 1]; // ?????????????,?
  375. }
  376. }
  377. if (prefix.length() > 0)
  378. prefix += '?'; // ????????,??????
  379. return prefix + suffix; // ??????
  380. }
  381. /**
  382. * ??????????????????
  383. *
  384. * @param str
  385. * ?????????????????????????
  386. * @return String ???????????????
  387. */
  388. private static String removeSameString(String str) {
  389. Set<String> mLinkedSet = new LinkedHashSet<String>();// set??????????????
  390. String[] strArray = str.split(" ");// ????(?????)?????
  391. StringBuffer sb = new StringBuffer();
  392. for (int i = 0; i < strArray.length; i++) {
  393. if (!mLinkedSet.contains(strArray[i])) {
  394. mLinkedSet.add(strArray[i]);
  395. sb.append(strArray[i] + " ");
  396. }
  397. }
  398. System.out.println(mLinkedSet);
  399. return sb.toString();
  400. }
  401. /**
  402. * ???????????
  403. *
  404. * @param src
  405. * @return
  406. */
  407. public static String encoding(String src) {
  408. if (src == null)
  409. return "";
  410. StringBuilder result = new StringBuilder();
  411. if (src != null) {
  412. src = src.trim();
  413. for (int pos = 0; pos < src.length(); pos++) {
  414. switch (src.charAt(pos)) {
  415. case '\"':
  416. result.append("&quot;");
  417. break;
  418. case '<':
  419. result.append("&lt;");
  420. break;
  421. case '>':
  422. result.append("&gt;");
  423. break;
  424. case '\'':
  425. result.append("&apos;");
  426. break;
  427. case '&':
  428. result.append("&amp;");
  429. break;
  430. case '%':
  431. result.append("&pc;");
  432. break;
  433. case '_':
  434. result.append("&ul;");
  435. break;
  436. case '#':
  437. result.append("&shap;");
  438. break;
  439. case '?':
  440. result.append("&ques;");
  441. break;
  442. default:
  443. result.append(src.charAt(pos));
  444. break;
  445. }
  446. }
  447. }
  448. return result.toString();
  449. }
  450. /**
  451. * ?????????????????
  452. *
  453. * @param handset
  454. * @return boolean
  455. */
  456. public static boolean isHandset(String handset) {
  457. try {
  458. String regex = "^1[\\d]{10}$";
  459. Pattern pattern = Pattern.compile(regex);
  460. Matcher matcher = pattern.matcher(handset);
  461. return matcher.matches();
  462. } catch (RuntimeException e) {
  463. return false;
  464. }
  465. }
  466. /**
  467. * ????????????
  468. *
  469. * @param src
  470. * @return
  471. */
  472. public static String decoding(String src) {
  473. if (src == null)
  474. return "";
  475. String result = src;
  476. result = result.replace("&quot;", "\"").replace("&apos;", "\'");
  477. result = result.replace("&lt;", "<").replace("&gt;", ">");
  478. result = result.replace("&amp;", "&");
  479. result = result.replace("&pc;", "%").replace("&ul", "_");
  480. result = result.replace("&shap;", "#").replace("&ques", "?");
  481. return result;
  482. }
  483. /**
  484. * @param args
  485. */
  486. public static void main(String[] args) {
  487. // String source = "abcdefgabcdefgabcdefgabcdefgabcdefgabcdefg";
  488. // String from = "efg";
  489. // String to = "???";
  490. // System.out.println("????source???to??from???????"
  491. // + replace(from, to, source));
  492. // System.out.println("?????????????"
  493. // + toLength("abcdefgabcdefgabcdefgabcdefgabcdefgabcdefg", 9));
  494. // System.out.println("????????" + isInteger("+0"));
  495. // System.out.println("???????????double?float?" + isDouble("+0.36"));
  496. // System.out.println("????????????Email???" +
  497. // isEmail("fhwbj@163.com"));
  498. // System.out.println("???????????????" + isChinese("???"));
  499. // System.out.println("?????????????" + isPrime(12));
  500. // System.out.println("?????????" + hangeToBig("10019658"));
  501. System.out.println("??????????????" + removeSameString("100 100 9658"));
  502. // System.out.println("???????" + encoding("100\"s<>fdsd100 9658"));
  503. // System.out.println("?????????????" + isHandset("15981807340"));
  504. // System.out.println("???????Email?" + parse("159818 fwhbj@163.com
  505. // 07340"));
  506. }
  507. }