判断字符串是否为空

雨点打透心脏的1/2处 2022-08-18 13:16 423阅读 0赞
  1. package com.paincupid.springmvc.controller;
  2. public class StringUtil {
  3. /** 空字符串。 */
  4. public static final String EMPTY_STRING = "";
  5. /* ============================================================================ */
  6. /* 判空函数。 */
  7. /* */
  8. /* 以下方法用来判定一个字符串是否为: */
  9. /* 1. null */
  10. /* 2. empty - "" */
  11. /* 3. blank - "全部是空白" - 空白由Character.isWhitespace所定义。 */
  12. /* ============================================================================ */
  13. /**
  14. * StringUtil.isEmpty(null) = true
  15. * StringUtil.isEmpty("") = true
  16. * StringUtil.isEmpty(" ") = false
  17. * StringUtil.isEmpty("bob") = false
  18. * StringUtil.isEmpty(" bob ") = false
  19. *
  20. * @param str 要检查的字符串
  21. *
  22. * @return 如果为空, 则返回<code>true</code>
  23. */
  24. public static boolean isEmpty(String str) {
  25. return ((str == null) || (str.length() == 0));
  26. }
  27. /**
  28. * StringUtil.isEmpty(null) = false
  29. * StringUtil.isEmpty("") = false
  30. * StringUtil.isEmpty(" ") = true
  31. * StringUtil.isEmpty("bob") = true
  32. * StringUtil.isEmpty(" bob ") = true
  33. *
  34. * @param str 要检查的字符串
  35. *
  36. * @return 如果不为空, 则返回<code>true</code>
  37. */
  38. public static boolean isNotEmpty(String str) {
  39. return ((str != null) && (str.length() > 0));
  40. }
  41. /**
  42. * StringUtil.isBlank(null) = true
  43. * StringUtil.isBlank("") = true
  44. * StringUtil.isBlank(" ") = true
  45. * StringUtil.isBlank("bob") = false
  46. * StringUtil.isBlank(" bob ") = false
  47. *
  48. * @return 如果为空白, 则返回<code>true</code>
  49. */
  50. public static boolean isBlank(String str) {
  51. int length;
  52. if ((str == null) || ((length = str.length()) == 0)) {
  53. return true;
  54. }
  55. for (int i = 0; i < length; i++) {
  56. if (!Character.isWhitespace(str.charAt(i))) {
  57. return false;
  58. }
  59. }
  60. return true;
  61. }
  62. /**
  63. * StringUtil.isBlank(null) = false
  64. * StringUtil.isBlank("") = false
  65. * StringUtil.isBlank(" ") = false
  66. * StringUtil.isBlank("bob") = true
  67. * StringUtil.isBlank(" bob ") = true
  68. *
  69. * @param str 要检查的字符串
  70. * @return 如果为空白, 则返回<code>true</code>
  71. */
  72. public static boolean isNotBlank(String str) {
  73. int length;
  74. if ((str == null) || ((length = str.length()) == 0)) {
  75. return false;
  76. }
  77. for (int i = 0; i < length; i++) {
  78. if (!Character.isWhitespace(str.charAt(i))) {
  79. return true;
  80. }
  81. }
  82. return false;
  83. }
  84. }

发表评论

表情:
评论列表 (有 0 条评论,423人围观)

还没有评论,来说两句吧...

相关阅读