StringUtil.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. package com.gyee.impala.common.util;
  2. import java.math.BigDecimal;
  3. import java.math.RoundingMode;
  4. import java.text.ParseException;
  5. import java.text.SimpleDateFormat;
  6. import java.util.*;
  7. import java.util.regex.Matcher;
  8. import java.util.regex.Pattern;
  9. /**
  10. * 字符串工具类
  11. *
  12. * @author fc
  13. */
  14. public class StringUtil extends org.apache.commons.lang3.StringUtils
  15. {
  16. /** 空字符串 */
  17. private static final String NULLSTR = "";
  18. /** 下划线 */
  19. private static final char SEPARATOR = '_';
  20. /**
  21. * 获取参数不为空值
  22. *
  23. * @param value defaultValue 要判断的value
  24. * @return value 返回值
  25. */
  26. public static <T> T nvl(T value, T defaultValue)
  27. {
  28. return value != null ? value : defaultValue;
  29. }
  30. /**
  31. * * 判断一个Collection是否为空, 包含List,Set,Queue
  32. *
  33. * @param coll 要判断的Collection
  34. * @return true:为空 false:非空
  35. */
  36. public static boolean isEmpty(Collection<?> coll)
  37. {
  38. return isNull(coll) || coll.isEmpty();
  39. }
  40. /**
  41. * * 判断一个Collection是否非空,包含List,Set,Queue
  42. *
  43. * @param coll 要判断的Collection
  44. * @return true:非空 false:空
  45. */
  46. public static boolean isNotEmpty(Collection<?> coll)
  47. {
  48. return !isEmpty(coll);
  49. }
  50. public static int roundToInt(double num) {
  51. return new BigDecimal(num).setScale(0, RoundingMode.HALF_UP).intValue();
  52. }
  53. /**
  54. * * 判断一个对象数组是否为空
  55. *
  56. * @param objects 要判断的对象数组
  57. ** @return true:为空 false:非空
  58. */
  59. public static boolean isEmpty(Object[] objects)
  60. {
  61. return isNull(objects) || (objects.length == 0);
  62. }
  63. /**
  64. * * 判断一个对象数组是否非空
  65. *
  66. * @param objects 要判断的对象数组
  67. * @return true:非空 false:空
  68. */
  69. public static boolean isNotEmpty(Object[] objects)
  70. {
  71. return !isEmpty(objects);
  72. }
  73. /**
  74. * * 判断一个Map是否为空
  75. *
  76. * @param map 要判断的Map
  77. * @return true:为空 false:非空
  78. */
  79. public static boolean isEmpty(Map<?, ?> map)
  80. {
  81. return isNull(map) || map.isEmpty();
  82. }
  83. /**
  84. * * 判断一个Map是否为空
  85. *
  86. * @param map 要判断的Map
  87. * @return true:非空 false:空
  88. */
  89. public static boolean isNotEmpty(Map<?, ?> map)
  90. {
  91. return !isEmpty(map);
  92. }
  93. /**
  94. * * 判断一个字符串是否为空串
  95. *
  96. * @param str String
  97. * @return true:为空 false:非空
  98. */
  99. public static boolean isEmpty(String str)
  100. {
  101. return isNull(str) || NULLSTR.equals(str.trim());
  102. }
  103. /**
  104. * * 判断一个字符串是否为非空串
  105. *
  106. * @param str String
  107. * @return true:非空串 false:空串
  108. */
  109. public static boolean isNotEmpty(String str)
  110. {
  111. return !isEmpty(str);
  112. }
  113. /**
  114. * * 判断一个对象是否为空
  115. *
  116. * @param object Object
  117. * @return true:为空 false:非空
  118. */
  119. public static boolean isNull(Object object)
  120. {
  121. return object == null;
  122. }
  123. /**
  124. * * 判断一个对象是否非空
  125. *
  126. * @param object Object
  127. * @return true:非空 false:空
  128. */
  129. public static boolean isNotNull(Object object)
  130. {
  131. return !isNull(object);
  132. }
  133. /**
  134. * * 判断一个对象是否是数组类型(Java基本型别的数组)
  135. *
  136. * @param object 对象
  137. * @return true:是数组 false:不是数组
  138. */
  139. public static boolean isArray(Object object)
  140. {
  141. return isNotNull(object) && object.getClass().isArray();
  142. }
  143. /**
  144. * 去空格
  145. */
  146. public static String trim(String str)
  147. {
  148. return (str == null ? "" : str.trim());
  149. }
  150. /**
  151. * 截取字符串
  152. *
  153. * @param str 字符串
  154. * @param start 开始
  155. * @return 结果
  156. */
  157. public static String substring(final String str, int start)
  158. {
  159. if (str == null)
  160. {
  161. return NULLSTR;
  162. }
  163. if (start < 0)
  164. {
  165. start = str.length() + start;
  166. }
  167. if (start < 0)
  168. {
  169. start = 0;
  170. }
  171. if (start > str.length())
  172. {
  173. return NULLSTR;
  174. }
  175. return str.substring(start);
  176. }
  177. /**
  178. * 截取字符串
  179. *
  180. * @param str 字符串
  181. * @param start 开始
  182. * @param end 结束
  183. * @return 结果
  184. */
  185. public static String substring(final String str, int start, int end)
  186. {
  187. if (str == null)
  188. {
  189. return NULLSTR;
  190. }
  191. if (end < 0)
  192. {
  193. end = str.length() + end;
  194. }
  195. if (start < 0)
  196. {
  197. start = str.length() + start;
  198. }
  199. if (end > str.length())
  200. {
  201. end = str.length();
  202. }
  203. if (start > end)
  204. {
  205. return NULLSTR;
  206. }
  207. if (start < 0)
  208. {
  209. start = 0;
  210. }
  211. if (end < 0)
  212. {
  213. end = 0;
  214. }
  215. return str.substring(start, end);
  216. }
  217. /**
  218. * 驼峰命名转下划线 nameVc>>name_vc
  219. */
  220. public static String toUnderScoreCase(String s)
  221. {
  222. if (s == null)
  223. {
  224. return null;
  225. }
  226. StringBuilder sb = new StringBuilder();
  227. boolean upperCase = false;
  228. for (int i = 0; i < s.length(); i++)
  229. {
  230. char c = s.charAt(i);
  231. boolean nextUpperCase = true;
  232. if (i < (s.length() - 1))
  233. {
  234. nextUpperCase = Character.isUpperCase(s.charAt(i + 1));
  235. }
  236. if ((i > 0) && Character.isUpperCase(c))
  237. {
  238. if (!upperCase || !nextUpperCase)
  239. {
  240. sb.append(SEPARATOR);
  241. }
  242. upperCase = true;
  243. }
  244. else
  245. {
  246. upperCase = false;
  247. }
  248. sb.append(Character.toLowerCase(c));
  249. }
  250. return sb.toString();
  251. }
  252. /**
  253. * 是否包含字符串
  254. *
  255. * @param str 验证字符串
  256. * @param strs 字符串组
  257. * @return 包含返回true
  258. */
  259. public static boolean inStringIgnoreCase(String str, String... strs)
  260. {
  261. if (str != null && strs != null)
  262. {
  263. for (String s : strs)
  264. {
  265. if (str.equalsIgnoreCase(trim(s)))
  266. {
  267. return true;
  268. }
  269. }
  270. }
  271. return false;
  272. }
  273. /**
  274. * 将下划线大写方式命名的字符串转换为驼峰式。如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。 例如:HELLO_WORLD->HelloWorld
  275. *
  276. * @param name 转换前的下划线大写方式命名的字符串
  277. * @return 转换后的驼峰式命名的字符串
  278. */
  279. public static String convertToCamelCase(String name)
  280. {
  281. StringBuilder result = new StringBuilder();
  282. // 快速检查
  283. if (name == null || name.isEmpty())
  284. {
  285. // 没必要转换
  286. return "";
  287. }
  288. else if (!name.contains("_"))
  289. {
  290. // 不含下划线,仅将首字母大写
  291. return name.substring(0, 1).toUpperCase() + name.substring(1);
  292. }
  293. // 用下划线将原始字符串分割
  294. String[] camels = name.split("_");
  295. for (String camel : camels)
  296. {
  297. // 跳过原始字符串中开头、结尾的下换线或双重下划线
  298. if (camel.isEmpty())
  299. {
  300. continue;
  301. }
  302. // 首字母大写
  303. result.append(camel.substring(0, 1).toUpperCase());
  304. result.append(camel.substring(1).toLowerCase());
  305. }
  306. return result.toString();
  307. }
  308. /**
  309. * 首字母大写
  310. *
  311. * @param name
  312. * @return
  313. */
  314. public static String firstUpperCase(String name) {
  315. name = name.substring(0, 1).toUpperCase() + name.substring(1);
  316. return name;
  317. }
  318. /**
  319. * 首字母小写
  320. *
  321. * @param name
  322. * @return
  323. */
  324. public static String firstLowerCase(String name) {
  325. name = name.substring(0, 1).toLowerCase() + name.substring(1);
  326. return name;
  327. }
  328. /**
  329. * 将下划线转化为大写
  330. *
  331. * @param name
  332. * @param firstCase 首字母是否大写 true:大写 false;小写
  333. * @return
  334. */
  335. public static String upperCase_(String name, boolean firstCase) {
  336. if(isEmpty(name)){
  337. return "";
  338. }
  339. String[] s = name.split("_");
  340. StringBuffer stringBuffer = new StringBuffer();
  341. for (String s1 : s) {
  342. stringBuffer.append(s1.substring(0, 1).toUpperCase() + s1.substring(1));
  343. }
  344. if(!firstCase){
  345. return firstLowerCase(stringBuffer.toString());
  346. }
  347. return stringBuffer.toString();
  348. }
  349. /**
  350. * get方法名字转成 t_b_abc>>tBAbc
  351. * @param str
  352. * @return
  353. * @author gyee
  354. * @Date 2020年1月30日 下午11:55:54
  355. */
  356. public static String toFUNName(String str) {
  357. StringBuffer buffer=new StringBuffer();
  358. String name=str;
  359. if(name.contains("_")) {
  360. // 用下划线将原始字符串分割
  361. String[] camels = name.split("_");
  362. boolean b=true;
  363. for (String str1 : camels) {
  364. if(str1.length()==1&&b) {
  365. b=false;
  366. buffer.append(str1);
  367. }else {
  368. buffer.append(StringUtil.firstUpperCase(str1));
  369. }
  370. }
  371. }else {
  372. buffer.append(StringUtil.firstUpperCase(name));
  373. }
  374. return buffer.toString();
  375. }
  376. /**
  377. * 是否不为空串
  378. *
  379. * @param obj
  380. * @return
  381. */
  382. public static boolean notEmp(Object obj) {
  383. return !empty(obj);
  384. }
  385. /**
  386. * 是否为空串
  387. *
  388. * @param obj
  389. * @return
  390. */
  391. public static boolean empty(Object obj) {
  392. if (obj == null)
  393. return true;
  394. String str;
  395. if (obj instanceof String) {
  396. str = (String) obj;
  397. } else {
  398. str = obj.toString();
  399. }
  400. return str.length() == 0;
  401. }
  402. public static double round(double num, int digit) {
  403. return new BigDecimal(num).setScale(digit, RoundingMode.HALF_UP).doubleValue();
  404. }
  405. /**
  406. * 将字符串解析为Date类型
  407. *
  408. * @param date
  409. * @param pattern
  410. * @return
  411. */
  412. public static Date toDate(String date, String pattern) {
  413. SimpleDateFormat format = new SimpleDateFormat(pattern);
  414. try {
  415. return format.parse(date);
  416. } catch (ParseException e) {
  417. throw new RuntimeException(String.format("Failed to parse the String [%s] to Date.", date), e);
  418. }
  419. }
  420. /**
  421. *
  422. * @方法名称: getUUID
  423. * @描述: 获得UUID
  424. * @参数 @return
  425. * @返回值 String
  426. * @抛出异常
  427. */
  428. public static String getUUID() {
  429. String s = UUID.randomUUID().toString();
  430. // 去掉“-”符号
  431. return s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18) + s.substring(19, 23) + s.substring(24);
  432. }
  433. /**
  434. * 判断是否为数字字符串
  435. * @param str
  436. * @return
  437. */
  438. public static boolean isNumeric(String str){
  439. Pattern pattern = Pattern.compile("^(-?\\d+)(\\.\\d+)?$");
  440. if(notEmp(str))
  441. {
  442. Matcher isNum = pattern.matcher(str);
  443. if( !isNum.matches() ){
  444. return false;
  445. }
  446. }else
  447. {
  448. return false;
  449. }
  450. return true;
  451. }
  452. /**
  453. * 正则表达式
  454. * @param input 需要匹配的字符串
  455. * @param expression 正则表达公式
  456. * @return
  457. */
  458. public static List<String> pattern(String input, String expression){
  459. // 创建 Pattern 对象
  460. Pattern p = Pattern.compile(expression);
  461. Matcher m = p.matcher(input);
  462. ArrayList list = new ArrayList();
  463. while (m.find()) {
  464. list.add(m.group(0));
  465. }
  466. //去除重复值
  467. HashSet hs=new HashSet(list);
  468. list.clear();
  469. list.addAll(hs);
  470. return list;
  471. }
  472. }