StringUtils.java 11 KB

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