StringUtil.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. package com.jsh.erp.utils;
  2. import org.springframework.util.StringUtils;
  3. import com.alibaba.fastjson.JSON;
  4. import com.alibaba.fastjson.JSONObject;
  5. import java.math.BigDecimal;
  6. import java.text.ParseException;
  7. import java.text.SimpleDateFormat;
  8. import java.util.*;
  9. import java.util.regex.Pattern;
  10. /**
  11. * @author longyong qq752718920 2018-10-7 15:26:27
  12. */
  13. public class StringUtil {
  14. private StringUtil() {
  15. }
  16. private static String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";
  17. public final static String regex = "'|#|%|;|--| and | and|and | or | or|or | not | not|not " +
  18. "| use | use|use | insert | insert|insert | delete | delete|delete | update | update|update " +
  19. "| select | select|select | count | count|count | group | group|group | union | union|union " +
  20. "| create | create|create | drop | drop|drop | truncate | truncate|truncate | alter | alter|alter " +
  21. "| grant | grant|grant | execute | execute|execute | exec | exec|exec | xp_cmdshell | xp_cmdshell|xp_cmdshell " +
  22. "| call | call|call | declare | declare|declare | source | source|source | sql | sql|sql ";
  23. /** 下划线 */
  24. private static final char SEPARATOR = '_';
  25. public static String filterNull(String str) {
  26. if (str == null) {
  27. return "";
  28. } else {
  29. return str.trim();
  30. }
  31. }
  32. public static boolean stringEquels(String source,String target) {
  33. if(isEmpty(source)||isEmpty(target)){
  34. return false;
  35. }else{
  36. return source.equals(target);
  37. }
  38. }
  39. public static int length(CharSequence cs) {
  40. return cs == null ? 0 : cs.length();
  41. }
  42. /**
  43. * 驼峰转下划线命名
  44. */
  45. public static String toUnderScoreCase(String str)
  46. {
  47. if (str == null)
  48. {
  49. return null;
  50. }
  51. StringBuilder sb = new StringBuilder();
  52. // 前置字符是否大写
  53. boolean preCharIsUpperCase = true;
  54. // 当前字符是否大写
  55. boolean curreCharIsUpperCase = true;
  56. // 下一字符是否大写
  57. boolean nexteCharIsUpperCase = true;
  58. for (int i = 0; i < str.length(); i++)
  59. {
  60. char c = str.charAt(i);
  61. if (i > 0)
  62. {
  63. preCharIsUpperCase = Character.isUpperCase(str.charAt(i - 1));
  64. }
  65. else
  66. {
  67. preCharIsUpperCase = false;
  68. }
  69. curreCharIsUpperCase = Character.isUpperCase(c);
  70. if (i < (str.length() - 1))
  71. {
  72. nexteCharIsUpperCase = Character.isUpperCase(str.charAt(i + 1));
  73. }
  74. if (preCharIsUpperCase && curreCharIsUpperCase && !nexteCharIsUpperCase)
  75. {
  76. sb.append(SEPARATOR);
  77. }
  78. else if ((i != 0 && !preCharIsUpperCase) && curreCharIsUpperCase)
  79. {
  80. sb.append(SEPARATOR);
  81. }
  82. sb.append(Character.toLowerCase(c));
  83. }
  84. return sb.toString();
  85. }
  86. /**
  87. * * 判断一个对象是否为空
  88. *
  89. * @param object Object
  90. * @return true:为空 false:非空
  91. */
  92. public static boolean isNull(Object object)
  93. {
  94. return object == null;
  95. }
  96. /**
  97. * * 判断一个对象是否非空
  98. *
  99. * @param object Object
  100. * @return true:非空 false:空
  101. */
  102. public static boolean isNotNull(Object object)
  103. {
  104. return !isNull(object);
  105. }
  106. public static boolean isEmpty(String str) {
  107. return str == null || "".equals(str.trim());
  108. }
  109. public static boolean isNotEmpty(String str) {
  110. return !isEmpty(str);
  111. }
  112. public static String getSysDate(String format) {
  113. if (StringUtil.isEmpty(format)) {
  114. format = DEFAULT_FORMAT;
  115. }
  116. SimpleDateFormat df = new SimpleDateFormat(format);
  117. return df.format(new Date());
  118. }
  119. public static Date getDateByString(String date, String format) {
  120. if (StringUtil.isEmpty(format)) {
  121. format = DEFAULT_FORMAT;
  122. }
  123. if (StringUtil.isNotEmpty(date)) {
  124. SimpleDateFormat sdf = new SimpleDateFormat(format);
  125. try {
  126. return sdf.parse(date);
  127. } catch (ParseException e) {
  128. throw new RuntimeException("转换为日期类型错误:DATE:" + date + " FORMAT:" + format);
  129. }
  130. } else {
  131. return null;
  132. }
  133. }
  134. public static Date getDateByLongDate(Long millis) {
  135. if (millis == null) {
  136. return new Date();
  137. }
  138. Calendar cal = Calendar.getInstance();
  139. cal.setTimeInMillis(millis);
  140. return cal.getTime();
  141. }
  142. public static UUID stringToUUID(String id) {
  143. if (StringUtil.isNotEmpty(id)) {
  144. return UUID.fromString(id);
  145. } else {
  146. return null;
  147. }
  148. }
  149. public static Integer parseInteger(String str) {
  150. if (StringUtil.isNotEmpty(str)) {
  151. return Integer.parseInt(str);
  152. } else {
  153. return null;
  154. }
  155. }
  156. public static Long parseStrLong(String str) {
  157. if (StringUtil.isNotEmpty(str)) {
  158. return Long.parseLong(str);
  159. } else {
  160. return null;
  161. }
  162. }
  163. public static List<UUID> listToUUID(List<String> listStrs) {
  164. if (listStrs != null && listStrs.size() > 0) {
  165. List<UUID> uuidList = new ArrayList<UUID>();
  166. for (String str : listStrs) {
  167. uuidList.add(UUID.fromString(str));
  168. }
  169. return uuidList;
  170. } else {
  171. return null;
  172. }
  173. }
  174. public static List<UUID> arrayToUUIDList(String[] uuids) {
  175. if (uuids != null && uuids.length > 0) {
  176. List<UUID> uuidList = new ArrayList<UUID>();
  177. for (String str : uuids) {
  178. uuidList.add(UUID.fromString(str));
  179. }
  180. return uuidList;
  181. } else {
  182. return null;
  183. }
  184. }
  185. //是否是JSON
  186. public static boolean containsAny(String str, String... flag) {
  187. if (str != null) {
  188. if (flag == null || flag.length == 0) {
  189. flag = "[-{-}-]-,".split("-");
  190. }
  191. for (String s : flag) {
  192. if (str.contains(s)) {
  193. return true;
  194. }
  195. }
  196. }
  197. return false;
  198. }
  199. public static String getModifyOrgOperateData(UUID resourceId, UUID orgId) {
  200. if (resourceId != null && orgId != null) {
  201. Map<UUID, UUID> map = new HashMap<UUID, UUID>();
  202. map.put(resourceId, orgId);
  203. return JSON.toJSONString(map);
  204. }
  205. return "";
  206. }
  207. public static String[] listToStringArray(List<String> list) {
  208. if (list != null && !list.isEmpty()) {
  209. return list.toArray(new String[list.size()]);
  210. }
  211. return new String[0];
  212. }
  213. public static Long[] listToLongArray(List<Long> list) {
  214. if (list != null && !list.isEmpty()) {
  215. return list.toArray(new Long[list.size()]);
  216. }
  217. return new Long[0];
  218. }
  219. public static List<String> stringToListArray(String[] strings) {
  220. if (strings != null && strings.length > 0) {
  221. return Arrays.asList(strings);
  222. }
  223. return new ArrayList<String>();
  224. }
  225. public static BigDecimal getArrSum(String[] strings) {
  226. BigDecimal sum = BigDecimal.ZERO;
  227. for(int i=0;i<strings.length;i++){
  228. sum = sum.add(new BigDecimal(strings[i]));
  229. }
  230. return sum;
  231. }
  232. /**
  233. * String字符串转成List<Long>数据格式
  234. * String str = "1,2,3,4,5,6" -> List<Long> listLong [1,2,3,4,5,6];
  235. *
  236. * @param strArr
  237. * @return
  238. */
  239. public static List<Long> strToLongList(String strArr) {
  240. List<Long> idList=new ArrayList<Long>();
  241. String[] d=strArr.split(",");
  242. for (int i = 0, size = d.length; i < size; i++) {
  243. if(d[i]!=null) {
  244. idList.add(Long.parseLong(d[i]));
  245. }
  246. }
  247. return idList;
  248. }
  249. /**
  250. * String字符串转成List<BigDecimal>数据格式
  251. * String str = "1,2,3,4,5,6" -> List<BigDecimal> listBigDecimal [1,2,3,4,5,6];
  252. *
  253. * @param strArr
  254. * @return
  255. */
  256. public static List<BigDecimal> strToBigDecimalList(String strArr) {
  257. List<BigDecimal> idList=new ArrayList<>();
  258. String[] d=strArr.split(",");
  259. for (int i = 0, size = d.length; i < size; i++) {
  260. if(d[i]!=null) {
  261. idList.add(new BigDecimal(d[i]));
  262. }
  263. }
  264. return idList;
  265. }
  266. /**
  267. * String字符串转成List<String>数据格式
  268. * String str = "1,2,3,4,5,6" -> List<Long> listLong [1,2,3,4,5,6];
  269. *
  270. * @param strArr
  271. * @return
  272. */
  273. public static List<String> strToStringList(String strArr) {
  274. if(StringUtils.isEmpty(strArr)){
  275. return null;
  276. }
  277. List<String> idList=new ArrayList<String>();
  278. String[] d=strArr.split(",");
  279. for (int i = 0, size = d.length; i < size; i++) {
  280. if(d[i]!=null) {
  281. idList.add(d[i].toString());
  282. }
  283. }
  284. return idList;
  285. }
  286. public static List<String> searchCondition(String search) {
  287. if (isEmpty(search)) {
  288. return new ArrayList<String>();
  289. }else{
  290. //String[] split = search.split(" ");
  291. String[] split = search.split("#");
  292. return stringToListArray(split);
  293. }
  294. }
  295. public static String getInfo(String search, String key){
  296. String value = null;
  297. if(StringUtil.isNotEmpty(search)) {
  298. search = search.replace("{}","");
  299. if(StringUtil.isNotEmpty(search)) {
  300. JSONObject obj = JSONObject.parseObject(search);
  301. if (obj.get(key) != null) {
  302. value = obj.getString(key).trim();
  303. if (value.equals("")) {
  304. value = null;
  305. }
  306. } else {
  307. value = null;
  308. }
  309. }
  310. }
  311. return value;
  312. }
  313. public static String toNull(String value) {
  314. if(isEmpty(value)) {
  315. value = null;
  316. } else {
  317. value = value.trim();
  318. }
  319. return value;
  320. }
  321. public static boolean isExist(Object value) {
  322. if(value!=null) {
  323. String str = value.toString();
  324. if("".equals(str.trim())) {
  325. return false;
  326. } else {
  327. return true;
  328. }
  329. } else {
  330. return false;
  331. }
  332. }
  333. /**
  334. * 判断对象是否为正整数
  335. * @param value
  336. * @return
  337. */
  338. public static boolean isPositiveLong(Object value) {
  339. if(value!=null) {
  340. String str = value.toString();
  341. if(isNotEmpty(str)) {
  342. if((str.matches("[0-9]+"))&&(Long.parseLong(str)>0)) {
  343. return true;
  344. } else {
  345. return false;
  346. }
  347. } else {
  348. return false;
  349. }
  350. } else {
  351. return false;
  352. }
  353. }
  354. /**
  355. * 校验条码长度为4到40位
  356. * @param value
  357. * @return
  358. */
  359. public static boolean checkBarCodeLength(Object value) {
  360. if(value!=null) {
  361. String str = value.toString();
  362. if(isNotEmpty(str)) {
  363. if(str.length()>=4 && str.length()<=40 ) {
  364. return true;
  365. } else {
  366. return false;
  367. }
  368. } else {
  369. return false;
  370. }
  371. } else {
  372. return false;
  373. }
  374. }
  375. /**
  376. * 判断对象是否为数字(含小数)
  377. * @param str
  378. * @return
  379. */
  380. public static boolean isPositiveBigDecimal(String str){
  381. Pattern pattern = Pattern.compile("[0-9]*");
  382. if(str.indexOf(".")>0){//判断是否有小数点
  383. if(str.indexOf(".")==str.lastIndexOf(".") && str.split("\\.").length==2){ //判断是否只有一个小数点
  384. return pattern.matcher(str.replace(".","")).matches();
  385. }else {
  386. return false;
  387. }
  388. }else {
  389. return pattern.matcher(str).matches();
  390. }
  391. }
  392. /**
  393. * sql注入过滤,保障sql的安全执行
  394. * @param originStr
  395. * @return
  396. */
  397. public static String safeSqlParse(String originStr){
  398. return originStr.replaceAll("(?i)" + regex, "");
  399. }
  400. /**
  401. * 判断字符串是否为纯数字
  402. * @param str 输入的字符串
  403. * @return 如果字符串为纯数字,返回 true;否则返回 false
  404. */
  405. public static boolean isNumeric(String str) {
  406. if (str == null || str.isEmpty()) {
  407. return false;
  408. }
  409. // 使用正则表达式判断字符串是否为纯数字
  410. return str.matches("\\d+");
  411. }
  412. }