StringUtil.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /*
  2. * 文件名:StringTools.java
  3. * 跟踪单号:
  4. * 修改单号:
  5. * 修改内容:
  6. */
  7. package com.java110.utils.util;
  8. import com.alibaba.fastjson.JSONObject;
  9. import java.io.UnsupportedEncodingException;
  10. import java.util.Date;
  11. import java.util.regex.Pattern;
  12. /**
  13. * String的工具类
  14. *
  15. * @author 吕振龙
  16. * @see StringUtil
  17. * @since
  18. */
  19. public class StringUtil {
  20. /**
  21. * Description: 格式化字符串(用户组建表使用) <br>
  22. *
  23. * @param name
  24. * @return String
  25. */
  26. public static String formatString(String name) {
  27. String s = "0000000000" + name;
  28. return s.substring(s.length() - 10, s.length());
  29. }
  30. /**
  31. * Description:把GBK转码成ISO8859_1 <br>
  32. *
  33. * @param name
  34. * @return String
  35. */
  36. public static String getStringByISO(String name) {
  37. try {
  38. // 防止中文字符出现乱码
  39. name = new String(name.getBytes("GBK"), "ISO8859_1");
  40. } catch (UnsupportedEncodingException e2) {
  41. }
  42. return name;
  43. }
  44. /**
  45. * Description:获得格式化的url <br>
  46. *
  47. * @param url
  48. * @return String
  49. */
  50. public static String getFormatUrl(String url) {
  51. if (isNullOrNone(url)) {
  52. return "";
  53. }
  54. if (url.indexOf('?') == -1) {
  55. return url + '?';
  56. } else {
  57. return url + '&';
  58. }
  59. }
  60. /**
  61. * Description: 获得字符(byte)的实际长度<br>
  62. * 1、…<br>
  63. * 2、…<br>
  64. * 3、…<br>
  65. * 4、…<br>
  66. *
  67. * @param s
  68. * @return int
  69. * @exception/throws
  70. */
  71. public static int lengthByte(String s) {
  72. int length = 0;
  73. for (int i = 0; i < s.length(); i++) {
  74. if (s.charAt(i) <= 127) {
  75. length++;
  76. } else {
  77. length = length + 2;
  78. }
  79. }
  80. return length;
  81. }
  82. /**
  83. * Description: 在jdbc用thin客户端连接oracle里面中文算3个字符<br>
  84. *
  85. * @param s
  86. * @return int
  87. * @exception/throws
  88. */
  89. public static int lengthByteInPrepared(String s) {
  90. int length = 0;
  91. if (s == null) {
  92. return length;
  93. }
  94. for (int i = 0; i < s.length(); i++) {
  95. if (s.charAt(i) <= 127) {
  96. length++;
  97. } else {
  98. length = length + 3;
  99. }
  100. }
  101. return length;
  102. }
  103. /**
  104. * Description:判断字段空null <br>
  105. *
  106. * @param s
  107. * @return boolean
  108. */
  109. public static boolean isNullOrNone(String s) {
  110. if (s == null || "".equals(s.trim())) {
  111. return true;
  112. }
  113. return false;
  114. }
  115. /**
  116. * Description: 判断对象是否为空<br>
  117. *
  118. * @param obj
  119. * @return boolean
  120. * @exception/throws [违例类型] [违例说明]
  121. */
  122. public static boolean isNullOrNone(Object obj) {
  123. if (obj == null) {
  124. return true;
  125. }
  126. if (obj instanceof String) {
  127. return isNullOrNone((String) obj);
  128. }
  129. return false;
  130. }
  131. /**
  132. * Description:判断字段空null <br>
  133. *
  134. * @param ss
  135. * @return boolean
  136. */
  137. public static boolean isNullOrNone(String[] ss) {
  138. if (ss == null || ss.length == 0) {
  139. return true;
  140. }
  141. for (int i = 0; i < ss.length; i++) {
  142. if (ss[i] == null || "".equals(ss[i].trim())) {
  143. return true;
  144. }
  145. }
  146. return false;
  147. }
  148. public static int countChar(String src, char c) {
  149. if (isNullOrNone(src)) {
  150. return 0;
  151. }
  152. int k = 0;
  153. for (int i = 0; i < src.length(); i++) {
  154. if (src.charAt(i) == c) {
  155. k++;
  156. }
  157. }
  158. return k;
  159. }
  160. /**
  161. * 把'变成 ''
  162. *
  163. * @param src
  164. * @return
  165. */
  166. public static String oracleString(String src) {
  167. if (isNullOrNone(src)) {
  168. return "";
  169. }
  170. return src.replaceAll("'", "''");
  171. }
  172. /**
  173. * 替换函数
  174. *
  175. * @param source
  176. * @param find
  177. * @param rep
  178. * @return String
  179. */
  180. public static String replace(String source, String find, String rep) {
  181. String result = source;
  182. String begin = "";
  183. String end = "";
  184. int flag;
  185. flag = result.indexOf(find, 0);
  186. while (flag != -1) {
  187. begin = result.substring(0, flag);
  188. end = result.substring(flag + (find.length()), result.length());
  189. result = begin + rep + end;
  190. flag = result.indexOf(find, flag + rep.length());
  191. }
  192. return result;
  193. }
  194. /**
  195. * 保护截断
  196. *
  197. * @param s
  198. * @param begin
  199. * @param end
  200. * @return
  201. */
  202. public static String truncate(String s, int begin, int end) {
  203. if (isNullOrNone(s)) {
  204. return "";
  205. }
  206. if (begin < 0) {
  207. return "";
  208. }
  209. if (begin >= end) {
  210. return "";
  211. }
  212. s = s.trim();
  213. if (begin >= s.length()) {
  214. return "";
  215. }
  216. if (end >= s.length()) {
  217. return s;
  218. } else {
  219. return s.substring(begin, end);
  220. }
  221. }
  222. /**
  223. * Description: 判断是否为数字<br>
  224. * 1、…<br>
  225. * 2、…<br>
  226. * 3、…<br>
  227. * 4、…<br>
  228. *
  229. * @param str
  230. * @return boolean
  231. * @exception/throws [违例类型] [违例说明]
  232. */
  233. public static boolean isNumber(String str) {
  234. if (null == str || "".equals(str)) {
  235. return false;
  236. } else {
  237. return Pattern.matches("^[0-9]+$", str);
  238. }
  239. }
  240. /**
  241. * Description: 获得字符(byte)的实际长度<br>
  242. * 1、…<br>
  243. * 2、…<br>
  244. * 3、…<br>
  245. * 4、…<br>
  246. *
  247. * @param s
  248. * @return int
  249. * @exception/throws
  250. */
  251. public static int realLength(String s) {
  252. if (null == s || "".equals(s)) {
  253. return 0;
  254. } else {
  255. return s.getBytes().length;
  256. }
  257. }
  258. /**
  259. * Description: 检查非法字符<br>
  260. */
  261. public static boolean isUnlawfulChar(String s) {
  262. if (null == s || "".equals(s)) {
  263. return false;
  264. } else {
  265. return Pattern.matches("^[^`~@#\\$%\\^&\\*\\(\\)=\\!\\+\\\\/\\|<>\\?;\\:\\.'\"\\{\\}\\[\\]??, ]*$", s);
  266. }
  267. }
  268. /**
  269. * Description: 判断是否为字母<br>
  270. * 1、…<br>
  271. * 2、…<br>
  272. * 3、…<br>
  273. * 4、…<br>
  274. *
  275. * @param str
  276. * @return boolean
  277. * @exception/throws [违例类型] [违例说明]
  278. */
  279. public static boolean isLetter(String str) {
  280. if (null == str || "".equals(str)) {
  281. return false;
  282. } else {
  283. return Pattern.matches("^[A-Za-z]+$", str);
  284. }
  285. }
  286. /**
  287. * (source != null) ? source : "";相当于oracle的nvl函数
  288. *
  289. * @param source
  290. * @return String
  291. */
  292. public static String nvl(String source) {
  293. return (source != null) ? source.trim() : "";
  294. }
  295. /**
  296. * Method nvl.
  297. *
  298. * @param source String
  299. * @param defaultString String
  300. * @return String
  301. */
  302. public static String nvl(String source, String defaultString) {
  303. return (source != null) ? source.trim() : defaultString;
  304. }
  305. /**
  306. * 实现对符合条件的字符串的反转
  307. *
  308. * @param str 原始字符串
  309. * @return 经过反转处理后的字符串
  310. */
  311. public static String reverseString(String str) {
  312. StringBuffer resultStringBuffer = new StringBuffer();
  313. String[] allStr = str.split(" ");
  314. StringBuffer sb = new StringBuffer(); // 保存需要进行反转的字符串
  315. for (int i = 0; i < allStr.length; i++) {
  316. if (meetReverseCondition(allStr[i])) {
  317. sb.delete(0, sb.length());
  318. // 将需要反转的字符串反转后添加到结果字符串
  319. resultStringBuffer.append(sb.append(allStr[i]).reverse()).append(" ");
  320. } else {
  321. resultStringBuffer.append(allStr[i]).append(" ");
  322. }
  323. }
  324. return resultStringBuffer.deleteCharAt(resultStringBuffer.length() - 1).toString();
  325. }
  326. /**
  327. * 判断字符串是否符合反转条件 本函数被reverseString(String str)调用
  328. *
  329. * @param str 需要进行反转处理的字符串
  330. * @return true 符合反转条件 false 不符合反转条件
  331. */
  332. private static boolean meetReverseCondition(String str) {
  333. // 如果字符串是以'*'开头并且长度大于1,并且第二个字符是英文,汉字或数字
  334. return (str.charAt(0) == '*') && (str.length() > 1) && Character.isLetterOrDigit(str.charAt(1));
  335. }
  336. /**
  337. * 取字符串的前maxLength长度的字串(一个中文字符为2个长度)
  338. *
  339. * @param str
  340. * @param maxLength
  341. * @return String throws
  342. */
  343. public static String splitStr(String str, int maxLength) {
  344. if (maxLength <= 0) {
  345. return "";
  346. }
  347. String result = str;
  348. byte[] bytes = str.getBytes();
  349. if (bytes.length > maxLength) {
  350. result = new String(bytes, 0, maxLength);
  351. if (!result.substring(result.length() - 1).equals(str.substring(result.length() - 1, result.length()))) {
  352. result = new String(bytes, 0, maxLength - 1);
  353. }
  354. }
  355. return result;
  356. }
  357. /**
  358. * 判断是否存在
  359. *
  360. * @param filterStr
  361. * @param id
  362. * @return boolean
  363. */
  364. public static boolean isExists(String filterStr, String id) {
  365. String[] filterTag = filterStr.split(",");
  366. for (int i = 0; i < filterTag.length; i++) {
  367. if (id.equals(filterTag[i])) {
  368. return true;
  369. }
  370. }
  371. return false;
  372. }
  373. /**
  374. * 格式化时间
  375. *
  376. * @param dt
  377. * @param format
  378. * @return String
  379. */
  380. public static String getTime(Date dt, String format) {
  381. String time = null;
  382. java.text.DateFormat df = new java.text.SimpleDateFormat(format);
  383. time = df.format(dt);
  384. return time;
  385. }
  386. public static String[] stringToArray(String str, String start, String end, String split) {
  387. if (null == str || "".equals(str))
  388. return null;
  389. if (str.indexOf(start) == -1 || str.indexOf(end) == -1 || str.indexOf(split) == -1)
  390. return new String[]{str};
  391. return str.substring(str.indexOf(start) + 1, str.indexOf(end)).split(split);
  392. }
  393. /**
  394. * 判断字符串是否为null或者空
  395. *
  396. * @param str
  397. * @return
  398. */
  399. public static boolean isEmpty(String str) {
  400. return str == null || "".equals(str);
  401. }
  402. /**
  403. * 获取指定长度(按字节长度获取)的字符串,中文算2个字节长度,兼容oracle的 varchar2长度计算方式
  404. *
  405. * @param src 源字符串
  406. * @param length 长度
  407. * @return
  408. */
  409. public static String getSubStr(String src, int length) {
  410. if (StringUtil.isEmpty(src)) {
  411. return null;
  412. }
  413. byte[] b = src.getBytes();
  414. if (b.length > length) {
  415. byte[] s = new byte[length];
  416. for (int i = 0; i < length; i++) {
  417. s[i] = b[i];
  418. }
  419. return new String(s);
  420. } else {
  421. return src;
  422. }
  423. }
  424. public static String truncateMessage(String description, int length) {
  425. org.springframework.util.Assert.state(length > 0);
  426. if ((description != null) && (description.length() > length)) {
  427. return description.substring(0, length);
  428. }
  429. return description;
  430. }
  431. /**
  432. * json是否包含key 并且存在值
  433. *
  434. * @param param
  435. * @param key
  436. * @return
  437. */
  438. public static boolean jsonHasKayAndValue(JSONObject param, String key) {
  439. if (param == null) {
  440. return false;
  441. }
  442. if (!param.containsKey(key)) {
  443. return false;
  444. }
  445. if (isEmpty(param.getString(key))) {
  446. return false;
  447. }
  448. return true;
  449. }
  450. /**
  451. * json是否包含key 并且存在值
  452. *
  453. * @param param
  454. * @param key
  455. * @return
  456. */
  457. public static boolean jsonHasKayAndValue(String param, String key) {
  458. JSONObject paramObj = null;
  459. try {
  460. paramObj = JSONObject.parseObject(param);
  461. return jsonHasKayAndValue(paramObj, key);
  462. } catch (Exception e) {
  463. return false;
  464. }
  465. }
  466. }