StringUtils.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. package cn.cslg.pas.common.utils;
  2. import lombok.extern.slf4j.Slf4j;
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. import java.util.List;
  6. import java.util.UUID;
  7. @Slf4j
  8. public class StringUtils {
  9. private final static int NUM_32 = 32;
  10. //集群号
  11. private static int machineId = 1;
  12. /**
  13. * 下划线
  14. */
  15. private static final char SEPARATOR = '_';
  16. /**
  17. * 下划线转驼峰命名
  18. */
  19. public static String toUnderScoreCase(String str) {
  20. if (str == null) {
  21. return null;
  22. }
  23. StringBuilder sb = new StringBuilder();
  24. // 前置字符是否大写
  25. boolean preCharIsUpperCase = true;
  26. // 当前字符是否大写
  27. boolean curreCharIsUpperCase = true;
  28. // 下一字符是否大写
  29. boolean nexteCharIsUpperCase = true;
  30. for (int i = 0; i < str.length(); i++) {
  31. char c = str.charAt(i);
  32. if (i > 0) {
  33. preCharIsUpperCase = Character.isUpperCase(str.charAt(i - 1));
  34. } else {
  35. preCharIsUpperCase = false;
  36. }
  37. curreCharIsUpperCase = Character.isUpperCase(c);
  38. if (i < (str.length() - 1)) {
  39. nexteCharIsUpperCase = Character.isUpperCase(str.charAt(i + 1));
  40. }
  41. if (preCharIsUpperCase && curreCharIsUpperCase && !nexteCharIsUpperCase) {
  42. sb.append(SEPARATOR);
  43. } else if ((i != 0 && !preCharIsUpperCase) && curreCharIsUpperCase) {
  44. sb.append(SEPARATOR);
  45. }
  46. sb.append(Character.toLowerCase(c));
  47. }
  48. return sb.toString();
  49. }
  50. /**
  51. * * 判断一个对象是否非空
  52. *
  53. * @param object Object
  54. * @return true:非空 false:空
  55. */
  56. public static boolean isNotNull(Object object) {
  57. return !isNull(object);
  58. }
  59. public static boolean isNull(Object object) {
  60. return object == null;
  61. }
  62. /**
  63. * 把String 转换为 long
  64. *
  65. * @param str
  66. * @param defaultData
  67. * @return
  68. */
  69. public static long getLong(String str, Long defaultData) {
  70. Long lnum = defaultData;
  71. if (isEmpty(str)) {
  72. return lnum;
  73. }
  74. try {
  75. lnum = Long.valueOf(str.trim()).longValue();
  76. } catch (NumberFormatException e) {
  77. log.warn("把String 转换为 long======== " + str);
  78. }
  79. return lnum;
  80. }
  81. /**
  82. * 转换成Boolean类型
  83. *
  84. * @param str
  85. * @param defaultData
  86. * @return
  87. */
  88. public static Boolean getBoolean(String str, Boolean defaultData) {
  89. Boolean lnum = defaultData;
  90. if (isEmpty(str)) {
  91. return lnum;
  92. }
  93. try {
  94. lnum = Boolean.valueOf(str.trim()).booleanValue();
  95. } catch (NumberFormatException e) {
  96. log.warn("把String 转换为 long======== " + str);
  97. }
  98. return lnum;
  99. }
  100. /**
  101. * 把String转换成int数据
  102. *
  103. * @param str
  104. * @param defaultData
  105. * @return
  106. */
  107. public static int getInt(String str, Integer defaultData) {
  108. int inum = defaultData;
  109. if (isEmpty(str)) {
  110. return inum;
  111. }
  112. try {
  113. inum = Integer.valueOf(str.trim()).intValue();
  114. } catch (NumberFormatException e) {
  115. log.warn("把String转换成int数据========== " + str);
  116. }
  117. return inum;
  118. }
  119. /**
  120. * 把String转换成double数据
  121. *
  122. * @param str
  123. * @param defaultData
  124. * @return
  125. */
  126. public static double getDouble(String str, Double defaultData) {
  127. double dnum = defaultData;
  128. if (isEmpty(str)) {
  129. return dnum;
  130. }
  131. try {
  132. dnum = Double.valueOf(str.trim()).doubleValue();
  133. } catch (NumberFormatException e) {
  134. log.error("把String转换成double数据: {}", str);
  135. }
  136. return dnum;
  137. }
  138. /**
  139. * 把String转换成float数据
  140. *
  141. * @param str
  142. * @param defaultData
  143. * @return
  144. */
  145. public static float getFloat(String str, Float defaultData) {
  146. float dnum = defaultData;
  147. if (isEmpty(str)) {
  148. return dnum;
  149. }
  150. try {
  151. dnum = Float.valueOf(str.trim()).floatValue();
  152. } catch (NumberFormatException e) {
  153. log.error("把String转换成float数据: {}", str);
  154. }
  155. return dnum;
  156. }
  157. /**
  158. * 判断字符串是否为空
  159. *
  160. * @param s
  161. * @return
  162. */
  163. public static Boolean isEmpty(String s) {
  164. if (s == null || s.length() <= 0) {
  165. return true;
  166. }
  167. return false;
  168. }
  169. /**
  170. * 判断字符串是否为空
  171. *
  172. * @param str
  173. * @return
  174. */
  175. public static boolean isNotEmpty(String str) {
  176. return !StringUtils.isEmpty(str);
  177. }
  178. /**
  179. * 按code截取字符串
  180. *
  181. * @return
  182. */
  183. public static String[] split(String str, String code) {
  184. String[] split;
  185. if (isEmpty(str)) {
  186. split = null;
  187. } else {
  188. split = str.split(code);
  189. }
  190. return split;
  191. }
  192. /**
  193. * 把字符串按code 转换为List<Long>
  194. *
  195. * @param str
  196. * @return
  197. */
  198. public static List<Long> changeStringToLong(String str, String code) {
  199. String[] split = split(str, code);
  200. List<Long> lnums = new ArrayList<>();
  201. for (String s : split) {
  202. if (!isEmpty(s)) {
  203. long lnum = getLong(s, 0L);
  204. lnums.add(lnum);
  205. }
  206. }
  207. return lnums;
  208. }
  209. /**
  210. * 把字符串按code 转换为List<String>
  211. *
  212. * @param str
  213. * @return
  214. */
  215. public static List<String> changeStringToString(String str, String code) {
  216. String[] split = split(str, code);
  217. List<String> lnums = new ArrayList<>();
  218. for (String s : split) {
  219. String trim = s.trim();
  220. lnums.add(trim);
  221. }
  222. return lnums;
  223. }
  224. /**
  225. * 把字符串按code 转换为List<Long>
  226. *
  227. * @param str
  228. * @return
  229. */
  230. public static List<Integer> changeStringToInteger(String str, String code) {
  231. if (isEmpty(str)) {
  232. return new ArrayList<>();
  233. }
  234. String[] split = split(str, code);
  235. List<Integer> inums = new ArrayList<>();
  236. for (String s : split) {
  237. int inum = getInt(s, 0);
  238. inums.add(inum);
  239. }
  240. return inums;
  241. }
  242. /**
  243. * 生成唯一订单号
  244. *
  245. * @return
  246. */
  247. public static String getOrderNumberByUUID() {
  248. int hashCodeV = UUID.randomUUID().toString().hashCode();
  249. //有可能是负数
  250. if (hashCodeV < 0) {
  251. hashCodeV = -hashCodeV;
  252. }
  253. String orderNumber = machineId + String.format("%015d", hashCodeV);
  254. return orderNumber;
  255. }
  256. /**
  257. * 生成唯一商户退款单号
  258. *
  259. * @return
  260. */
  261. public static String getOutRefundNoByUUID() {
  262. int hashCodeV = UUID.randomUUID().toString().hashCode();
  263. //有可能是负数
  264. if (hashCodeV < 0) {
  265. hashCodeV = -hashCodeV;
  266. }
  267. String out_refund_no = "BACK" + machineId + String.format("%015d", hashCodeV);
  268. return out_refund_no;
  269. }
  270. /**
  271. * 获取UUID,去掉了-
  272. *
  273. * @return
  274. */
  275. public static String getUUID() {
  276. String uuid = UUID.randomUUID().toString().replaceAll("-", "");
  277. log.debug("获取32位的UUID的调试日志-->>" + uuid);
  278. return uuid;
  279. }
  280. /**
  281. * 获取雪花UID
  282. *
  283. * @return
  284. */
  285. // public static Long getSnowflakeId() {
  286. // SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker(0, 0);
  287. // return snowflakeIdWorker.nextId();
  288. // }
  289. /**
  290. * list小于0的数据就过滤了
  291. * 把list的数组变成1,3,4,5,6
  292. *
  293. * @param list
  294. * @param code
  295. * @return
  296. */
  297. public static String listToString(List<Long> list, String code) {
  298. String s = "";
  299. if (list == null || list.size() <= 0) {
  300. return s;
  301. }
  302. for (Long l : list) {
  303. if (l.longValue() > 0) {
  304. s = s + l + code;
  305. }
  306. }
  307. return s;
  308. }
  309. /**
  310. * 按code把list的数组转换成字符串
  311. *
  312. * @param list
  313. * @param code
  314. * @return
  315. */
  316. public static String listTranformString(List<String> list, String code) {
  317. String s = "";
  318. if (list == null || list.size() <= 0) {
  319. return s;
  320. }
  321. s = String.join(code, list);
  322. return s;
  323. }
  324. /**
  325. * 判断是否为非空字符串
  326. *
  327. * @param str
  328. * @return
  329. */
  330. public static boolean isNotBlank(String str) {
  331. return !StringUtils.isBlank(str);
  332. }
  333. /**
  334. * 校验uid列表,检查里面元素是否满足限定长度为32
  335. *
  336. * @param collection
  337. * @return
  338. */
  339. public static boolean checkUidList(Collection<String> collection) {
  340. if (collection.size() == 0) {
  341. return false;
  342. }
  343. for (String uid : collection) {
  344. if (uid.trim().length() != NUM_32) {
  345. return false;
  346. }
  347. }
  348. return true;
  349. }
  350. /**
  351. * 判断是否为空字符串
  352. *
  353. * @param str
  354. * @return
  355. */
  356. public static boolean isBlank(String str) {
  357. int strLen;
  358. if (str == null || (strLen = str.length()) == 0) {
  359. return true;
  360. }
  361. for (int i = 0; i < strLen; i++) {
  362. if ((Character.isWhitespace(str.charAt(i)) == false)) {
  363. return false;
  364. }
  365. }
  366. return true;
  367. }
  368. /**
  369. * 判断一个字符串是否为数字
  370. *
  371. * @param str
  372. * @return
  373. */
  374. public static boolean isNumeric(String str) {
  375. try {
  376. //把字符串强制转换为数字
  377. Integer.valueOf(str);
  378. //如果是数字,返回True
  379. return true;
  380. } catch (Exception e) {
  381. //如果抛出异常,返回False
  382. return false;
  383. }
  384. }
  385. /**
  386. * 判断是不是整数数字
  387. * @param s
  388. * @return
  389. */
  390. public static boolean isInteger(String s) {
  391. if (s == null) {
  392. return false;
  393. }
  394. return s.matches("-?\\d+"); // 匹配整数,包括负数
  395. }
  396. /**
  397. * 判断是不是正整数数字
  398. * @param s
  399. * @return
  400. */
  401. public static boolean isPositiveInteger(String s) {
  402. if (s == null) {
  403. return false;
  404. }
  405. // 正则表达式匹配一个或多个正整数
  406. return s.matches("\\d+"); // "\\d+" 匹配一个或多个数字,但不包括负数和小数点
  407. }
  408. /**
  409. * 某个子串是否在字符串内
  410. *
  411. * @param str
  412. * @param searchChar
  413. * @return
  414. */
  415. public static boolean contains(String str, String searchChar) {
  416. if (isEmpty(str)) {
  417. return false;
  418. }
  419. return str.indexOf(searchChar) >= 0;
  420. }
  421. /**
  422. * 切割字符串
  423. *
  424. * @param str
  425. * @param start
  426. * @return
  427. */
  428. public static String substring(String str, int start) {
  429. if (str == null) {
  430. return null;
  431. }
  432. // handle negatives, which means last n characters
  433. if (start < 0) {
  434. start = str.length() + start;
  435. }
  436. if (start < 0) {
  437. start = 0;
  438. }
  439. if (start > str.length()) {
  440. return "";
  441. }
  442. return str.substring(start);
  443. }
  444. /**
  445. * 判断评论是否为垃圾评论(仅通过单一字符重复出现来判断,以后可以扩展更多的检测方法)
  446. *
  447. * @param content
  448. * @return
  449. */
  450. public static Boolean isCommentSpam(String content) {
  451. if (content == null) {
  452. return true;
  453. }
  454. char[] chars = content.toCharArray();
  455. // 最大重复次数
  456. Integer maxCount = 4;
  457. for (int a = 0; a < chars.length; a++) {
  458. Integer count = 1;
  459. for (int b = a; b < chars.length - 1; b++) {
  460. if (chars[b + 1] == chars[b]) {
  461. count++;
  462. // 判断字符重复的次数是否大于阈值
  463. if (count >= maxCount) {
  464. return true;
  465. }
  466. continue;
  467. } else {
  468. break;
  469. }
  470. }
  471. }
  472. return false;
  473. }
  474. public static <T> String join(List<T> list, String separator) {
  475. return org.apache.commons.lang3.StringUtils.join(list, separator);
  476. }
  477. public static <T> String join(String[] list, String separator) {
  478. return org.apache.commons.lang3.StringUtils.join(list, separator);
  479. }
  480. public static List<String> splitChineseText(String text,Integer textNum) {
  481. List<String> chunks = new ArrayList<>();
  482. int start = 0;
  483. int textLength = text.length();
  484. if (text.length() < textNum) {
  485. chunks.add(text);
  486. return chunks;
  487. }
  488. while (start < textLength) {
  489. // 计算当前块的最大可能结束位置
  490. int end = Math.min(start + textNum, textLength);
  491. int lastSentenceEnd = -1;
  492. // 从后向前查找最后一个句号
  493. for (int i = end - 1; i >= start; i--) {
  494. if (text.charAt(i) == '。') {
  495. lastSentenceEnd = i;
  496. break;
  497. }
  498. }
  499. if (lastSentenceEnd != -1) {
  500. // 切割文本并添加到结果集
  501. chunks.add(text.substring(start, lastSentenceEnd + 1));
  502. start = lastSentenceEnd + 1;
  503. } else {
  504. // 异常处理:当前2000字符范围内没有句号
  505. throw new RuntimeException("文本位置 " + start + " 至 " + end + " 之间未找到句号,请检查输入文本格式");
  506. }
  507. }
  508. return chunks;
  509. }
  510. }