StringUtils.java 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package top.lvzhiqiang.util;
  2. /**
  3. * 字符串处理工具类
  4. *
  5. * @author shiyong
  6. * 2021/10/25 10:48
  7. */
  8. public class StringUtils extends org.apache.commons.lang3.StringUtils {
  9. // 百分号,SQL参数中的任意长度通配符
  10. private static final String PERCENT = "%";
  11. // 下划线,SQL参数中的单个字符通配符
  12. private static final String UNDERLINE = "_";
  13. /**
  14. * 判断字符串是否不为空
  15. *
  16. * @param str 字符串
  17. * @return boolean
  18. * @author shiyong
  19. * 2019/10/16 16:15
  20. */
  21. public static boolean isNotEmpty(String str){
  22. return null != str && !"".equals(str.trim());
  23. }
  24. /**
  25. * 判断字符串是否为空
  26. *
  27. * @param str 字符串
  28. * @return boolean
  29. * @author shiyong
  30. * 2019/10/16 16:16
  31. */
  32. public static boolean isEmpty(String str) {
  33. return !isNotEmpty(str);
  34. }
  35. /**
  36. * 转义SQL参数中的通配符
  37. *
  38. * @param raw 原字符串
  39. * @return escaped sql parameter
  40. * @author ziyan.li
  41. * @since 15:00 2021/10/27
  42. */
  43. public static String escapeSqlParam(String raw) {
  44. if (raw == null) {
  45. return null;
  46. }
  47. // 转义通配符
  48. String processed = raw.replace(PERCENT, "\\" + PERCENT)
  49. .replace(UNDERLINE, "\\" + UNDERLINE);
  50. // 转义后去除前后空格
  51. return strip(processed);
  52. }
  53. public static String escapeJavParam(String raw) {
  54. if (raw == null) {
  55. return null;
  56. }
  57. // 转义通配符
  58. String processed = raw.replace("/", "")
  59. .replace("?", "?")
  60. .replace("%", "%")
  61. .replace("#","");
  62. // 转义后去除前后空格
  63. return strip(processed);
  64. }
  65. }